转自  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包,

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

三.使用

  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工具,用于抓取网络协议用的。很有帮助

转 Android HttpClient post MultipartEntity - Android 上传文件的更多相关文章

  1. Android应用开发中webview上传文件的几种思路

    1. 常规方法,重写WebChromeClient 的 openFileChooser 方法 private class MyWebChromeClient extends WebChromeClie ...

  2. Android通过HTTP协议实现上传文件数据

    SocketHttpRequester.java package cn.itcast.utils; import java.io.BufferedReader; import java.io.Byte ...

  3. HttpClient 测试web API上传文件实例

    1.使用HttpClient 测试上传文件并且设置header信息: using Lemon.Common; using Newtonsoft.Json; using System; using Sy ...

  4. C# HttpClient Post 参数同时上传文件 上传图片 调用接口

    // 调用接口上传文件 using (var client = new HttpClient()) { using (var multipartFormDataContent = new Multip ...

  5. httpclient请求接口,上传文件附加参数(.net core)

    /// <summary> /// 上传文件 - 武汉站点 /// </summary> [HttpPost] public IActionResult UploadWH(Re ...

  6. (十)HttpClient以multipart/form-data上传文件

    原文链接:https://blog.csdn.net/wsdtq123/article/details/78888734 POST上传文件 最早的HTTP POST是不支持文件上传的,给编程开发带来很 ...

  7. Android HttpClient post MultipartEntity - Android 上传文件

    转自[http://blog.csdn.net/hellohaifei/article/details/9707089] 在Android 中使用HttpClient,MultipartEntity ...

  8. WebAPI通过multipart/form-data方式同时上传文件以及数据(含HttpClient上传Demo)

    简单的Demo,用于了解WebAPI如何同时接收文件及数据,同时提供HttpClient模拟如何同时上传文件和数据的Demo,下面是HttpClient上传的Demo界面 1.HttpClient部分 ...

  9. 转 Android网络编程之使用HttpClient批量上传文件 MultipartEntityBuilder

    请尊重他人的劳动成果,转载请注明出处:Android网络编程之使用HttpClient批量上传文件 http://www.tuicool.com/articles/Y7reYb 我曾在<Andr ...

随机推荐

  1. python2.6.6在centos6.4下安装

    1.wget http://www.python.org/ftp/python/2.6.6/Python-2.6.6.tar.bz2 2. tar xvjf Python-2.6.6.tar.bz2 ...

  2. C# 系统应用之清除Cookies、IE临时文件、历史记录 转载

    http://blog.csdn.net/Eastmount/article/details/18821221 本文主要是项目"个人电脑使用记录清除软件"系类文章中关于清除浏览器C ...

  3. PHP扩展开发-简单类扩展

    今天来学习简单类扩展开发 实现目标为如下php的类 <?php class classext(){ private $username; CONST URL="http://www.g ...

  4. Hibernate写入Oracle Date类型处理

    Hibernate写入Oracle数据库时,数据库设计字段为Date类型时,只能保存年月日,不能保存时分秒,如果要保存时分秒,需修改Hibernate.cfg.xml文件 <property n ...

  5. Inno Setup入门(七)——提供安装语言选项

    Inno Setup安装目录下有一个Languages的文件夹,该文件夹提供了可供使用的语言,通过在脚本中加入[languages]段,可以实现该项功能,实现代码如下: [setup] ;全局设置,本 ...

  6. AVFoundation(一)---AVAudioPlayer

    AVAudioPlayer相当于一个播放器,它支持多种音频格式,而且能够进行进度.音量.播放速度等控制. 下边通过代码来看一下,它的属性和常用方法(具体说明都写在了注释中): //AVAudioPla ...

  7. ASP MVC之参数传递

    1.URL获取参数  Request.QueryString["XqType"]; 2.表单提交,control层获取参数:Request.Form["XXX" ...

  8. oracle_一次移动数据库dbf文件的操作

    oracle数据库的dbf路径下面磁盘不足,需要把原始路径下面的dbf文件移动到另外一个磁盘路径下, 具体的操作有四步. 1.把整个表空间offline. 2.copy原始路径下的dbf文件到新的路径 ...

  9. cfedu/A/求和

    题目连接 思路: 用数组直接标记2^n,n属于(0~~31);用LL或者INT都可以,不会爆.但是ans要用LL. #include <set> #include <map> ...

  10. Swift中的闭包(Closure) 浅析

    转载自:http://www.devtalking.com/articles/closure-expressions-in-swift/ 闭包在Swift中非常有用.通俗的解释就是一个Int类型里存储 ...