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 plain copy
 
  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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM