httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)

 

1. 首先需要对应的JAR包 导入 httpmime-4.1.1.jar。

package url;  
  1.   
  2. import io.IoStreamUtil;  
  3.   
  4. import java.io.File;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7.   
  8. import org.apache.http.HttpEntity;  
  9. import org.apache.http.HttpResponse;  
  10. import org.apache.http.client.ClientProtocolException;  
  11. import org.apache.http.client.HttpClient;  
  12. import org.apache.http.client.methods.HttpPost;  
  13. import org.apache.http.entity.mime.MultipartEntity;  
  14. import org.apache.http.entity.mime.content.FileBody;  
  15. import org.apache.http.entity.mime.content.StringBody;  
  16. import org.apache.http.impl.client.DefaultHttpClient;  
  17.   
  18. /** 
  19.  * httpclient上传文件 
  20.  * @author linwei 
  21.  * 
  22.  */  
  23. public class MultipartEntityUtil {  
  24.   
  25.     public static String postFile(File file,String url) throws ClientProtocolException, IOException {  
  26.   
  27.         FileBody bin = null;  
  28.         HttpClient httpclient = new DefaultHttpClient();  
  29.         HttpPost httppost = new HttpPost(url);  
  30.         if(file != null) {  
  31.             bin = new FileBody(file);  
  32.         }  
  33.   
  34.         StringBody username = new StringBody("13696900475");  
  35.         StringBody password = new StringBody("13696900475");  
package url;

import io.IoStreamUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * httpclient上传文件
 * @author linwei
 *
 */
public class MultipartEntityUtil {

	public static String postFile(File file,String url) throws ClientProtocolException, IOException {

		FileBody bin = null;
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);
		if(file != null) {
			bin = new FileBody(file);
		}

		StringBody username = new StringBody("13696900475");
		StringBody password = new StringBody("13696900475");
  1. //请记住,这边传递汉字会出现乱码,解决方法如下,设置好编码格式就好  
                //请记住,这边传递汉字会出现乱码,解决方法如下,设置好编码格式就好
  1.                //new StringBody("汉字",Charset.forName("UTF-8")));     
  2.       
  3.     MultipartEntity reqEntity = new MultipartEntity();  
  4.     reqEntity.addPart("username", username);  
  5.     reqEntity.addPart("password", password);  
  6.     reqEntity.addPart("data", bin);  
  7.       
  8.     httppost.setEntity(reqEntity);  
  9.     System.out.println("执行: " + httppost.getRequestLine());  
  10.       
  11.     HttpResponse response = httpclient.execute(httppost);  
  12.     System.out.println("statusCode is " + response.getStatusLine().getStatusCode());  
  13.     HttpEntity resEntity = response.getEntity();  
  14.     System.out.println("----------------------------------------");  
  15.     System.out.println(response.getStatusLine());  
  16.     if (resEntity != null) {  
  17.       System.out.println("返回长度: " + resEntity.getContentLength());  
  18.       System.out.println("返回类型: " + resEntity.getContentType());  
  19.       InputStream in = resEntity.getContent();  
  20.       System.out.println("in is " + in);  
  21.       System.out.println(IoStreamUtil.getStringFromInputStream(in));  
  22.     }  
  23.     if (resEntity != null) {  
  24.       resEntity.consumeContent();  
  25.     }  
  26.     return null;  
  27. }  
  28.   
  29. public static void main(String[] args) throws ClientProtocolException, IOException {  
  30.   
  31.     File file = new File("d:/rss.xml");  
  32.     String url = "http://localhost:8080/webtest/servlet/URLTest";  
  33.     postFile(file,url);  
  34. }  
  35.   
                //new StringBody("汉字",Charset.forName("UTF-8")));  
		
	    MultipartEntity reqEntity = new MultipartEntity();
	    reqEntity.addPart("username", username);
	    reqEntity.addPart("password", password);
	    reqEntity.addPart("data", bin);
	    
	    httppost.setEntity(reqEntity);
	    System.out.println("执行: " + httppost.getRequestLine());
	    
	    HttpResponse response = httpclient.execute(httppost);
	    System.out.println("statusCode is " + response.getStatusLine().getStatusCode());
	    HttpEntity resEntity = response.getEntity();
	    System.out.println("----------------------------------------");
	    System.out.println(response.getStatusLine());
	    if (resEntity != null) {
	      System.out.println("返回长度: " + resEntity.getContentLength());
	      System.out.println("返回类型: " + resEntity.getContentType());
	      InputStream in = resEntity.getContent();
	      System.out.println("in is " + in);
	      System.out.println(IoStreamUtil.getStringFromInputStream(in));
	    }
	    if (resEntity != null) {
	      resEntity.consumeContent();
	    }
		return null;
	}
	
	public static void main(String[] args) throws ClientProtocolException, IOException {
	
		File file = new File("d:/rss.xml");
		String url = "http://localhost:8080/webtest/servlet/URLTest";
		postFile(file,url);
	}
	
}


2. 服务端的接收解析代码(同样的,服务端也需要导入上面提到的JAR包) (特别注释:在servlet中直接获取的request是可以正常解析的,但是,在ACTION中获取的request则是通过ACTION内部的类进行了封装,因此,在ACTION中执行以下代码,upload.parseRequest(request)方法执行返回的是空的,暂没找到解决办法。)

 

  1.                 //commons-fileupload.jar  commons-io    
  2. <SPAN style="WHITE-SPACE: pre">     </SPAN>request.setCharacterEncoding("UTF-8");  
  3. <SPAN style="WHITE-SPACE: pre">     </SPAN>boolean isMultipart = ServletFileUpload.isMultipartContent(request);  
  4. <SPAN style="WHITE-SPACE: pre">     </SPAN>  
  5. <SPAN style="WHITE-SPACE: pre">     </SPAN>if (isMultipart) {    
  6.             FileItemFactory factory = new DiskFileItemFactory();    
  7.             ServletFileUpload upload = new ServletFileUpload(factory);    
  8.             try {     
  9.                 List items = upload.parseRequest(request);    
  10.                 Iterator iter = items.iterator();    
  11.                 while (iter.hasNext()) {    
  12.                     FileItem item = (FileItem) iter.next();    
  13.                     if (item.isFormField()) {    
  14.                         //普通文本信息处理     
  15.                         String paramName = item.getFieldName();    
  16.                         String paramValue = item.getString();    
  17.                         System.out.println(paramName + ":" + paramValue);    
  18.                     } else {    
  19.                         //上传文件信息处理     
  20.                         String fileName = item.getName();    
  21.                         byte[] data = item.get();    
  22.                         String filePath = "d:/ssssss.txt";    
  23.                         FileOutputStream fos = new FileOutputStream(filePath);    
  24.                         fos.write(data);    
  25.                         fos.close();    
  26.                     }    
  27.                 }     
  28.             } catch (FileUploadException e) {    
  29.                 e.printStackTrace();    
  30.             }    
  31.         }    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值