1. 程式人生 > >Android中使用HttpPost上傳圖片和資料

Android中使用HttpPost上傳圖片和資料

1、首先需要對應的JAR包 匯入 httpmime-4.1.1.jar

  1. package url;  
  2. import io.IoStreamUtil;  
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import org.apache.http.HttpEntity;  
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.client.ClientProtocolException;  
  9. import
     org.apache.http.client.HttpClient;  
  10. import org.apache.http.client.methods.HttpPost;  
  11. import org.apache.http.entity.mime.MultipartEntity;  
  12. import org.apache.http.entity.mime.content.FileBody;  
  13. import org.apache.http.entity.mime.content.StringBody;  
  14. import org.apache.http.impl.client.DefaultHttpClient;  
  15. /** 
  16.  * httpclient上傳檔案 
  17.  * @author linwei 
  18.  * 
  19.  */
  20. publicclass MultipartEntityUtil {  
  21.     publicstatic String postFile(File file,String url) throws ClientProtocolException, IOException {  
  22.         FileBody bin = null;  
  23.         HttpClient httpclient = new DefaultHttpClient();  
  24.         HttpPost httppost = new
     HttpPost(url);  
  25.         if(file != null) {  
  26.             bin = new FileBody(file);  
  27.         }  
  28.         StringBody username = new StringBody("13696900475");  
  29.         StringBody password = new StringBody("13696900475");  
  30. //請記住,這邊傳遞漢字會出現亂碼,解決方法如下,設定好編碼格式就好
  31.                 //new StringBody("漢字",Charset.forName("UTF-8")));  
  32.         MultipartEntity reqEntity = new MultipartEntity();  
  33.         reqEntity.addPart("username", username);  
  34.         reqEntity.addPart("password", password);  
  35.         reqEntity.addPart("data", bin);  
  36.         httppost.setEntity(reqEntity);  
  37.         System.out.println("執行: " + httppost.getRequestLine());  
  38.         HttpResponse response = httpclient.execute(httppost);  
  39.         System.out.println("statusCode is " + response.getStatusLine().getStatusCode());  
  40.         HttpEntity resEntity = response.getEntity();  
  41.         System.out.println("----------------------------------------");  
  42.         System.out.println(response.getStatusLine());  
  43.         if (resEntity != null) {  
  44.           System.out.println("返回長度: " + resEntity.getContentLength());  
  45.           System.out.println("返回型別: " + resEntity.getContentType());  
  46.           InputStream in = resEntity.getContent();  
  47.           System.out.println("in is " + in);  
  48.           System.out.println(IoStreamUtil.getStringFromInputStream(in));  
  49.         }  
  50.         if (resEntity != null) {  
  51.           resEntity.consumeContent();  
  52.         }  
  53.         returnnull;  
  54.     }  
  55.     publicstaticvoid main(String[] args) throws ClientProtocolException, IOException {  
  56.         File file = new File("d:/rss.xml");  
  57.         String url = "http://localhost:8080/webtest/servlet/URLTest";  
  58.         postFile(file,url);  
  59.     }  
  60. }  

2. 服務端的接收解析程式碼(同樣的,服務端也需要匯入上面提到的JAR包) (特別註釋:在servlet中直接獲取的request是可以正常解析的,但是,在ACTION中獲取的request則是通過ACTION內部的類進行了封裝,因此,在ACTION中執行以下程式碼,upload.parseRequest(request)方法執行返回的是空的,暫沒找到解決辦法。)

  1. //commons-fileupload.jar  commons-io 
  2. request.setCharacterEncoding("UTF-8");  
  3. boolean isMultipart = ServletFileUpload.isMultipartContent(request);  
  4.           FileItemFactory factory = new DiskFileItemFactory();    
  5.             ServletFileUpload upload = new ServletFileUpload(factory);    
  6.             try {     
  7.                 List items = upload.parseRequest(request);    
  8.                 Iterator iter = items.iterator();    
  9.                 while (iter.hasNext()) {    
  10.                     FileItem item = (FileItem) iter.next();    
  11.                     if (item.isFormField()) {    
  12.                         //普通文字資訊處理  
  13.                         String paramName = item.getFieldName();    
  14.                         String paramValue = item.getString();    
  15.                         System.out.println(paramName + ":" + paramValue);    
  16.                     } else {    
  17.                         //上傳檔案資訊處理  
  18.                         String fileName = item.getName();    
  19.                         byte[] data = item.get();    
  20.                         String filePath = "d:/ssssss.txt";    
  21.                         FileOutputStream fos = new FileOutputStream(filePath);    
  22.                         fos.write(data);    
  23.                         fos.close();    
  24.                     }    
  25.                 }     
  26.             } catch (FileUploadException e) {    
  27.                 e.printStackTrace();    
  28.             }    
  29.         }