转 Android HttpClient post MultipartEntity - Android 上传文件

时间:2023-11-23 17:56:56

转自  http://blog.****.net/hellohaifei/article/details/9707089

在Android 中使用HttpClient,MultipartEntity

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

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

转 Android HttpClient post MultipartEntity - Android 上传文件

可以只下载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包,

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

转 Android HttpClient post MultipartEntity - Android 上传文件

三.使用

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

四.与HttpURLConnection比较

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

附:

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