转 Android HttpClient post MultipartEntity - Android 上传文件
2015-03-17 16:02
633 查看
转自 /Uploads/Images/Content/201503/cd7efe929c89d581656bbb2e27e3e017
在Android 中使用HttpClient,MultipartEntity
为了发送图片,文件等资源,现在采用开源的org.apache.http.entity.mime.MultipartEntity
可以只下载binary,如果可能需要修改源文件的话,可以直接下载source.
将httpmime-4.2.5.jar包,放到android工程的lib目录下。
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,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 plaincopyclass 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 HttpClient post MultipartEntity - Android 上传文件
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件(二)
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件(二)
- android向服务器上传multipart/form-data文件(upload using multipart post using httpclient in android)
- HttpClient使用详解(MultipartEntityBuilder 上传文件等)
- 使用httpClient MultipartRequestEntity文件上传解析文件和普通表单参数
- HttpClient 4.3.6 使用MultipartEntityBuilder实现类似form表单提交方式的文件上传
- HttpClient使用MultipartEntityBuilder实现多文件上传
- Android HttpClient post MultipartEntity upload.
- Android HttpClient post MultipartEntity
- HttpClient MultipartRequestEntity文件上传
- httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)
- Android基于http协议和httpClient上传文件
- httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)
- Android_HttpClient_get请求post表单提交上传
- httpclient post 上传文件及参数包括中文乱码问题方案