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

来源:互联网 发布:淘宝贷款需要什么条件 编辑:程序博客网 时间:2024/06/05 05:50

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

[java] view plaincopyprint?
  1. package url;  
  2.   
  3. import io.IoStreamUtil;  
  4.   
  5. import java.io.File;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8.   
  9. import org.apache.http.HttpEntity;  
  10. import org.apache.http.HttpResponse;  
  11. import org.apache.http.client.ClientProtocolException;  
  12. import org.apache.http.client.HttpClient;  
  13. import org.apache.http.client.methods.HttpPost;  
  14. import org.apache.http.entity.mime.MultipartEntity;  
  15. import org.apache.http.entity.mime.content.FileBody;  
  16. import org.apache.http.entity.mime.content.StringBody;  
  17. import org.apache.http.impl.client.DefaultHttpClient;  
  18.   
  19. /** 
  20.  * httpclient上传文件 
  21.  * @author linwei 
  22.  * 
  23.  */  
  24. public class MultipartEntityUtil {  
  25.   
  26.     public static String postFile(File file,String url) throws ClientProtocolException, IOException {  
  27.   
  28.         FileBody bin = null;  
  29.         HttpClient httpclient = new DefaultHttpClient();  
  30.         HttpPost httppost = new HttpPost(url);  
  31.         if(file != null) {  
  32.             bin = new FileBody(file);  
  33.         }  
  34.   
  35.         StringBody username = new StringBody("13696900475");  
  36.         StringBody password = new StringBody("13696900475");  
[java] view plaincopyprint?
  1. //请记住,这边传递汉字会出现乱码,解决方法如下,设置好编码格式就好  
[java] view plaincopyprint?
  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.   


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

 

[java] view plaincopyprint?
  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.         }