Android HttpClient post MultipartEntity - Android 上传文件

转自[http://blog.csdn.net/hellohaifei/article/details/9707089]

在Android 中使用HttpClient,MultipartEntity

为了发送图片,文件等资源,现在采用开源的org.apache.http.entity.mime.MultipartEntity

一.去官网http://hc.apache.org/downloads.cgi 下载

可以只下载binary,如果可能需要修改源文件的话,可以直接下载source.

二.导入jar包

将下载下来的httpcomponents-client-4.2.5-bin.zip取其httpcomponents-client-4.2.5-bin.zip\httpcomponents-client-4.2.5\lib\httpmime-4.2.5.jar包

将httpmime-4.2.5.jar包,放到android工程的lib目录下。

三. 查看jar包,

我这里用的是源文件,因为我需要修改些东西

三.使用

 

[java] view plaincopy
 
  1. class MyAsyncTask extends AsyncTask<String, Integer, String> {  
  2.   
  3.         String FORM_TABLE_NAME = "ask?action=Chatbottom-toSay-";// 自己需要配置的表单  
  4.         String filePath = "/mnt/sdcard/picture.jpg";// 测试写的文件路径,转换成自己的文件路径  
  5.         final String hostUrl = "http://www.myhost.com";// 写成自己要上传的地址  
  6.   
  7.         @Override  
  8.         protected String doInBackground(String... params) {  
  9.             HttpClient httpclient = null;  
  10.             httpclient = new DefaultHttpClient();  
  11.   
  12.             final HttpPost httppost = new HttpPost(hostUrl);  
  13.             final File imageFile = new File(filePath);  
  14.             final MultipartEntity multipartEntity = new MultipartEntity();  
  15.   
  16.             if (false) {  
  17.                 InputStream in = null;  
  18.                 try {  
  19.                     in = new FileInputStream(imageFile);  
  20.                 } catch (FileNotFoundException e) {  
  21.                     e.printStackTrace();  
  22.                 }  
  23.                 InputStreamBody inputStreamBody = new InputStreamBody(in,  
  24.                         "android_inputstream.jpg");  
  25.                 // FormBodyPart formBodyPart = new FormBodyPart(FORM_TABLE_NAME,  
  26.                 // contentBody);  
  27.                 multipartEntity.addPart(FORM_TABLE_NAME, inputStreamBody);  
  28.             }  
  29.             if (false) {  
  30.                 ContentBody contentBody = new FileBody(imageFile);  
  31.                 FormBodyPart formBodyPart = new FormBodyPart(FORM_TABLE_NAME,  
  32.                         contentBody);  
  33.                 multipartEntity.addPart(formBodyPart);  
  34.             }  
  35.             if (false) {  
  36.                 // FileBody fileBody = new FileBody(imageFile, "image/jpeg",  
  37.                 // "utf-8");  
  38.                 FileBody fileBody = new FileBody(imageFile);  
  39.                 multipartEntity.addPart(FORM_TABLE_NAME, fileBody);  
  40.             }  
  41.   
  42.             if (true) {  
  43.                 Bitmap photoBM = BitmapFactory.decodeFile(filePath);  
  44.                 if (photoBM == null) {  
  45.                     return null;  
  46.                 }  
  47.                 ByteArrayOutputStream photoBao = new ByteArrayOutputStream();  
  48.                 boolean successCompress = photoBM.compress(CompressFormat.JPEG,  
  49.                         80, photoBao);  
  50.                 if (!successCompress) {  
  51.                     return null;  
  52.                 }  
  53.                 ByteArrayBody byteArrayBody = new ByteArrayBody(  
  54.                         photoBao.toByteArray(), "android.jpg");  
  55.                 photoBM.recycle();  
  56.                 // InputStreamBody inbody = new InputStreamBody(new InputStream,  
  57.                 // filename);  
  58.                 multipartEntity.addPart(FORM_TABLE_NAME, byteArrayBody);  
  59.             }  
  60.   
  61.             httppost.setEntity(multipartEntity);  
  62.   
  63.             HttpResponse httpResponse;  
  64.             try {  
  65.                 httpResponse = httpclient.execute(httppost);  
  66.   
  67.                 final int statusCode = httpResponse.getStatusLine()  
  68.                         .getStatusCode();  
  69.   
  70.                 String response = EntityUtils.toString(  
  71.                         httpResponse.getEntity(), HTTP.UTF_8);  
  72.                 IWLog.d("got response:\n" + response);  
  73.   
  74.                 if (statusCode == HttpStatus.SC_OK) {  
  75.                     return "success";  
  76.                 }  
  77.   
  78.             } catch (ClientProtocolException e) {  
  79.                 e.printStackTrace();  
  80.             } catch (IOException e) {  
  81.                 e.printStackTrace();  
  82.             } finally {  
  83.                 if (httpclient != null) {  
  84.                     httpclient.getConnectionManager().shutdown();  
  85.                     httpclient = null;  
  86.                 }  
  87.             }  
  88.             return null;  
  89.   
  90.         }  
  91.   
  92.         @Override  
  93.         protected void onPostExecute(String result) {  
  94.             super.onPostExecute(result);  
  95.             if (result.equals("success")) {  
  96.   
  97.             }  
  98.         }  
  99.   
  100.     }  

四.与HttpURLConnection比较

 

网上好多人都用的是HttpURLConnection来上传图片,文件。由于我在解决实际问题时HttpURLConnection并不能达到预期,老是死在urlConnection.getInputStream()永远回不来。所以不得以改用的上面的库。最终感觉MultipartEntity用起来比较简单。

附:

在解决实际问题中,我也不是一帆风顺,也遇到了各种抽象的问题。推荐给大家个工具wireshark工具,用于抓取网络协议用的。很有帮助

更多0
posted @ 2014-07-03 09:51  bruceHuang  阅读(16625)  评论(1编辑  收藏  举报