Android中使用HttpPost上传图片和数据

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

[java]  view plain copy
  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");  
  37. //请记住,这边传递汉字会出现乱码,解决方法如下,设置好编码格式就好  
  38.                 //new StringBody("汉字",Charset.forName("UTF-8")));    
  39.           
  40.         MultipartEntity reqEntity = new MultipartEntity();  
  41.         reqEntity.addPart("username", username);  
  42.         reqEntity.addPart("password", password);  
  43.         reqEntity.addPart("data", bin);  
  44.           
  45.         httppost.setEntity(reqEntity);  
  46.         System.out.println("执行: " + httppost.getRequestLine());  
  47.           
  48.         HttpResponse response = httpclient.execute(httppost);  
  49.         System.out.println("statusCode is " + response.getStatusLine().getStatusCode());  
  50.         HttpEntity resEntity = response.getEntity();  
  51.         System.out.println("----------------------------------------");  
  52.         System.out.println(response.getStatusLine());  
  53.         if (resEntity != null) {  
  54.           System.out.println("返回长度: " + resEntity.getContentLength());  
  55.           System.out.println("返回类型: " + resEntity.getContentType());  
  56.           InputStream in = resEntity.getContent();  
  57.           System.out.println("in is " + in);  
  58.           System.out.println(IoStreamUtil.getStringFromInputStream(in));  
  59.         }  
  60.         if (resEntity != null) {  
  61.           resEntity.consumeContent();  
  62.         }  
  63.         return null;  
  64.     }  
  65.       
  66.     public static void main(String[] args) throws ClientProtocolException, IOException {  
  67.       
  68.         File file = new File("d:/rss.xml");  
  69.         String url = "http://localhost:8080/webtest/servlet/URLTest";  
  70.         postFile(file,url);  
  71.     }  
  72.       
  73. }  
  74.     

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值