转 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包,
我这里用的是源文件,因为我需要修改些东西
三.使用
- class MyAsyncTask extends AsyncTask<String, Integer, String> {
- String FORM_TABLE_NAME = "ask?action=Chatbottom-toSay-";// 自己需要配置的表单
- String filePath = "/mnt/sdcard/picture.jpg";// 测试写的文件路径,转换成自己的文件路径
- final String hostUrl = "http://www.myhost.com";// 写成自己要上传的地址
- @Override
- protected String doInBackground(String... params) {
- HttpClient httpclient = null;
- httpclient = new DefaultHttpClient();
- final HttpPost httppost = new HttpPost(hostUrl);
- final File imageFile = new File(filePath);
- final MultipartEntity multipartEntity = new MultipartEntity();
- if (false) {
- InputStream in = null;
- try {
- in = new FileInputStream(imageFile);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- InputStreamBody inputStreamBody = new InputStreamBody(in,
- "android_inputstream.jpg");
- // FormBodyPart formBodyPart = new FormBodyPart(FORM_TABLE_NAME,
- // contentBody);
- multipartEntity.addPart(FORM_TABLE_NAME, inputStreamBody);
- }
- if (false) {
- ContentBody contentBody = new FileBody(imageFile);
- FormBodyPart formBodyPart = new FormBodyPart(FORM_TABLE_NAME,
- contentBody);
- multipartEntity.addPart(formBodyPart);
- }
- if (false) {
- // FileBody fileBody = new FileBody(imageFile, "image/jpeg",
- // "utf-8");
- FileBody fileBody = new FileBody(imageFile);
- multipartEntity.addPart(FORM_TABLE_NAME, fileBody);
- }
- if (true) {
- Bitmap photoBM = BitmapFactory.decodeFile(filePath);
- if (photoBM == null) {
- return null;
- }
- ByteArrayOutputStream photoBao = new ByteArrayOutputStream();
- boolean successCompress = photoBM.compress(CompressFormat.JPEG,
- 80, photoBao);
- if (!successCompress) {
- return null;
- }
- ByteArrayBody byteArrayBody = new ByteArrayBody(
- photoBao.toByteArray(), "android.jpg");
- photoBM.recycle();
- // InputStreamBody inbody = new InputStreamBody(new InputStream,
- // filename);
- multipartEntity.addPart(FORM_TABLE_NAME, byteArrayBody);
- }
- httppost.setEntity(multipartEntity);
- HttpResponse httpResponse;
- try {
- httpResponse = httpclient.execute(httppost);
- final int statusCode = httpResponse.getStatusLine()
- .getStatusCode();
- String response = EntityUtils.toString(
- httpResponse.getEntity(), HTTP.UTF_8);
- IWLog.d("got response:\n" + response);
- if (statusCode == HttpStatus.SC_OK) {
- return "success";
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (httpclient != null) {
- httpclient.getConnectionManager().shutdown();
- httpclient = null;
- }
- }
- return null;
- }
- @Override
- protected void onPostExecute(String result) {
- super.onPostExecute(result);
- if (result.equals("success")) {
- }
- }
- }
四.与HttpURLConnection比较
网上好多人都用的是HttpURLConnection来上传图片,文件。由于我在解决实际问题时HttpURLConnection并不能达到预期,老是死在urlConnection.getInputStream()永远回不来。所以不得以改用的上面的库。最终感觉MultipartEntity用起来比较简单。
附:
在解决实际问题中,我也不是一帆风顺,也遇到了各种抽象的问题。推荐给大家个工具wireshark工具,用于抓取网络协议用的。很有帮助
转 Android HttpClient post MultipartEntity - Android 上传文件的更多相关文章
- Android应用开发中webview上传文件的几种思路
1. 常规方法,重写WebChromeClient 的 openFileChooser 方法 private class MyWebChromeClient extends WebChromeClie ...
- Android通过HTTP协议实现上传文件数据
SocketHttpRequester.java package cn.itcast.utils; import java.io.BufferedReader; import java.io.Byte ...
- HttpClient 测试web API上传文件实例
1.使用HttpClient 测试上传文件并且设置header信息: using Lemon.Common; using Newtonsoft.Json; using System; using Sy ...
- C# HttpClient Post 参数同时上传文件 上传图片 调用接口
// 调用接口上传文件 using (var client = new HttpClient()) { using (var multipartFormDataContent = new Multip ...
- httpclient请求接口,上传文件附加参数(.net core)
/// <summary> /// 上传文件 - 武汉站点 /// </summary> [HttpPost] public IActionResult UploadWH(Re ...
- (十)HttpClient以multipart/form-data上传文件
原文链接:https://blog.csdn.net/wsdtq123/article/details/78888734 POST上传文件 最早的HTTP POST是不支持文件上传的,给编程开发带来很 ...
- Android HttpClient post MultipartEntity - Android 上传文件
转自[http://blog.csdn.net/hellohaifei/article/details/9707089] 在Android 中使用HttpClient,MultipartEntity ...
- WebAPI通过multipart/form-data方式同时上传文件以及数据(含HttpClient上传Demo)
简单的Demo,用于了解WebAPI如何同时接收文件及数据,同时提供HttpClient模拟如何同时上传文件和数据的Demo,下面是HttpClient上传的Demo界面 1.HttpClient部分 ...
- 转 Android网络编程之使用HttpClient批量上传文件 MultipartEntityBuilder
请尊重他人的劳动成果,转载请注明出处:Android网络编程之使用HttpClient批量上传文件 http://www.tuicool.com/articles/Y7reYb 我曾在<Andr ...
随机推荐
- docker 基础命令二
开启/停止/重启 查看当前正在运行容器docker ps 查看包括已经停止的所有容器docker ps -a 显示最新启动的一个容器docker ps -l 新建一个容器运行docker run 启动 ...
- JVM 设置
按照基本回收策略分 引用计数(Reference Counting) 标记-清除(Mark-Sweep) 复制(Copying) 标记-整理(Mark-Compact) 按分区对待的方式分 增量收集( ...
- UIView的基本属性及ANimation
frame属性:可以使用该属性改变尺寸和位置 相对于父视图bounds:改变尺寸 相对自身center:改变视图的位置alpha:改变视图的透明度backgroundColor:改变视图的背景cont ...
- DISUBSTR - Distinct Substrings
DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its dist ...
- hdu5269 ZYB loves Xor I
分治法和字典树都可以,都是递归,但字典树耗内存 从第一bit开始,若相同则xor为0,分到同一部分,不相同则统计,且此时lowbit为这一bit,最后结果要乘以2 /*分治法*/ #include&l ...
- UVA 571 Jugs ADD18 小白书10 数学Part1 专题
只能往一个方向倒,如c1=3,c2=5,a b从0 0->0 5->3 2->0 2->2 0->2 5->3 4->0 4->3 1->0 1- ...
- AsyncTask异步加载和HttpURLConnection网络请求数据
//获得网络数据 private void huodeshuju() { //这里是使用线程,已注释掉 /*new Thread(){ public void ...
- jquery_api(事件一)
一 .unload在火狐,谷歌无法弹出alerta是因为这两个浏览器默认组织alert弹出,unload事件可以进行一些对象销毁,事件解除绑定等清理工作. 如果你想在用户离开页面之前确认是否离开,最好 ...
- AngularJS 从零开始学习(一)
什么是AngularJS? AngularJS是一个把HTML(视图)绑定到JavaScript对象(模型)上的框架.当模型改变时,页面也能自动随之更新,反之亦然.当某个域的内容发生变化时,与之关联的 ...
- POJ - 3666 Making the Grade(dp+离散化)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...