轉自[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工具,用於抓取網絡協議用的。很有幫助
- 頂
