Java使用HttpURLConnection上傳文件(轉)


從普通Web頁面上傳文件很簡單,只需要在form標簽叫上enctype="multipart/form-data"即可,剩余工作便都交給瀏覽器去完成數據收集並發送Http請求。但是如果沒有頁面的話要怎么上傳文件呢?

由於脫離了瀏覽器的環境,我們就要自己去完成數據的收集並發送請求,所以就很麻煩了。首先我們來寫個JSP頁面並看看瀏覽器發出的Http請求是什么樣的

JSP頁面:

[html]  view plain  copy
 
  1. <html>  
  2.  <head>  
  3.   <meta charset="UTF-8">  
  4.   <title>TestSubmit</title>  
  5.  </head>  
  6.  <body>  
  7.   <form name="upform" action="upload.do" method="POST" enctype="multipart/form-data">  
  8.   參數<input type="text" name="username"/><br/>  
  9.   文件1<input type="file" name="file1"/><br/>  
  10.   文件2<input type="file" name="file2"/><br/>  
  11.   <input type="submit" value="Submit" /><br/>  
  12.   </form>  
  13.  </body>  
  14. </html>  

假如我參數寫的內容是hello word,然后二個文件是二個簡單的txt文件,form提交的信息為:

[plain]  view plain  copy
 
  1. -----------------------------7da2e536604c8  
  2. Content-Disposition: form-data; name="username"  
  3.   
  4. hello word  
  5. -----------------------------7da2e536604c8  
  6. Content-Disposition: form-data; name="file1"; filename="D:/haha.txt"  
  7. Content-Type: text/plain  
  8.   
  9. haha  
  10. hahaha  
  11. -----------------------------7da2e536604c8  
  12. Content-Disposition: form-data; name="file2"; filename="D:/huhu.txt"  
  13. Content-Type: text/plain  
  14.   
  15. messi  
  16. huhu  
  17. -----------------------------7da2e536604c8--  

研究下規律發現有如下幾點特征:

1. 第一行是“-----------------------------7da2e536604c8”作為分隔符,然后是“/r/n”回車換行符。 這個7da2e536604c8分隔符瀏覽器是隨機生成的。

2. 第二行是Content-Disposition: form-data; name="username"。代表form表單的數據域,name對應頁面input標簽的name值。

3. 第三行是“/r/n”回車換行符。

4. 第四行是參數username的值。

5. 第五行是7da2e536604c8分隔符。

6. 從第六行到第十行和從第十二行到第十六行,分別是上傳的兩個文件的數據域。

7. 第十二行是Content-Disposition: form-data; name="file2"; filename="D:/huhu.txt"。name對應頁面input標簽的name值,filename對應要上傳的文件名(包括路徑在內)。

8. 第十三行如果是文件就有Content-Type: text/plain。這里上傳的是txt文件所以是text/plain,如果上穿的是jpg圖片的話就是image/jpg了,可以自己試試看看。然后就是回車換行符。

9. 第十五、十六行就是文件的內容了。如:

[plain]  view plain  copy
 
  1. messi  
  2. huhu  

10. 最后一行是-----------------------------7da2e536604c8--。注意最后多了二個“--”,作為結束的標志。

 

那么我們只要模擬這個數據,並寫入到Http請求中便能實現文件的上傳。

其實,在我之前的文章:HttpClient使用詳解 ,就已經有利用HttpClient工具包上傳文件的例子,HttpClient是Apache的一個強大的模擬並發送所有Http請求的開源類庫,有時間的,大家可以學習學習,但本篇文章中,並不以HttpClient為例,而是采用Java自帶的HttpURLConnection實現的。

[java]  view plain  copy
 
  1. import java.io.BufferedReader;  
  2. import java.io.DataInputStream;  
  3. import java.io.DataOutputStream;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.InputStreamReader;  
  7. import java.io.OutputStream;  
  8. import java.net.HttpURLConnection;  
  9. import java.net.URL;  
  10. import java.util.HashMap;  
  11. import java.util.Iterator;  
  12. import java.util.Map;  
  13.   
  14. import net.sf.jmimemagic.Magic;  
  15. import net.sf.jmimemagic.MagicMatch;  
  16.   
  17. public class HttpPostUploadUtil {  
  18.   
  19.     /** 
  20.      * @param args 
  21.      */  
  22.     public static void main(String[] args) {  
  23.         String filepath = "E:\\ziliao\\0.jpg";  
  24.         String urlStr = "http://127.0.0.1:8080/minicms/up/up_result.jsp";  
  25.         Map<String, String> textMap = new HashMap<String, String>();  
  26.         textMap.put("name", "testname");  
  27.         Map<String, String> fileMap = new HashMap<String, String>();  
  28.         fileMap.put("userfile", filepath);  
  29.         String ret = formUpload(urlStr, textMap, fileMap);  
  30.         System.out.println(ret);  
  31.     }  
  32.   
  33.     /** 
  34.      * 上傳圖片 
  35.      * @param urlStr 
  36.      * @param textMap 
  37.      * @param fileMap 
  38.      * @return 
  39.      */  
  40.     public static String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap) {  
  41.         String res = "";  
  42.         HttpURLConnection conn = null;  
  43.         String BOUNDARY = "---------------------------123821742118716"; //boundary就是request頭和上傳文件內容的分隔符    
  44.         try {  
  45.             URL url = new URL(urlStr);  
  46.             conn = (HttpURLConnection) url.openConnection();  
  47.             conn.setConnectTimeout(5000);  
  48.             conn.setReadTimeout(30000);  
  49.             conn.setDoOutput(true);  
  50.             conn.setDoInput(true);  
  51.             conn.setUseCaches(false);  
  52.             conn.setRequestMethod("POST");  
  53.             conn.setRequestProperty("Connection", "Keep-Alive");  
  54.             conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");  
  55.             conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);  
  56.   
  57.             OutputStream out = new DataOutputStream(conn.getOutputStream());  
  58.             // text    
  59.             if (textMap != null) {  
  60.                 StringBuffer strBuf = new StringBuffer();  
  61.                 Iterator<Map.Entry<String, String>> iter = textMap.entrySet().iterator();  
  62.                 while (iter.hasNext()) {  
  63.                     Map.Entry<String, String> entry = iter.next();  
  64.                     String inputName = (String) entry.getKey();  
  65.                     String inputValue = (String) entry.getValue();  
  66.                     if (inputValue == null) {  
  67.                         continue;  
  68.                     }  
  69.                     strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");  
  70.                     strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");  
  71.                     strBuf.append(inputValue);  
  72.                 }  
  73.                 out.write(strBuf.toString().getBytes());  
  74.             }  
  75.   
  76.             // file    
  77.             if (fileMap != null) {  
  78.                 Iterator<Map.Entry<String, String>> iter = fileMap.entrySet().iterator();  
  79.                 while (iter.hasNext()) {  
  80.                     Map.Entry<String, String> entry = iter.next();  
  81.                     String inputName = (String) entry.getKey();  
  82.                     String inputValue = (String) entry.getValue();  
  83.                     if (inputValue == null) {  
  84.                         continue;  
  85.                     }  
  86.                     File file = new File(inputValue);  
  87.                     String filename = file.getName();  
  88.                     MagicMatch match = Magic.getMagicMatch(file, false, true);  
  89.                     String contentType = match.getMimeType();  
  90.   
  91.                     StringBuffer strBuf = new StringBuffer();  
  92.                     strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");  
  93.                     strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" + filename + "\"\r\n");  
  94.                     strBuf.append("Content-Type:" + contentType + "\r\n\r\n");  
  95.   
  96.                     out.write(strBuf.toString().getBytes());  
  97.   
  98.                     DataInputStream in = new DataInputStream(new FileInputStream(file));  
  99.                     int bytes = 0;  
  100.                     byte[] bufferOut = new byte[1024];  
  101.                     while ((bytes = in.read(bufferOut)) != -1) {  
  102.                         out.write(bufferOut, 0, bytes);  
  103.                     }  
  104.                     in.close();  
  105.                 }  
  106.             }  
  107.   
  108.             byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();  
  109.             out.write(endData);  
  110.             out.flush();  
  111.             out.close();  
  112.   
  113.             // 讀取返回數據    
  114.             StringBuffer strBuf = new StringBuffer();  
  115.             BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));  
  116.             String line = null;  
  117.             while ((line = reader.readLine()) != null) {  
  118.                 strBuf.append(line).append("\n");  
  119.             }  
  120.             res = strBuf.toString();  
  121.             reader.close();  
  122.             reader = null;  
  123.         } catch (Exception e) {  
  124.             System.out.println("發送POST請求出錯。" + urlStr);  
  125.             e.printStackTrace();  
  126.         } finally {  
  127.             if (conn != null) {  
  128.                 conn.disconnect();  
  129.                 conn = null;  
  130.             }  
  131.         }  
  132.         return res;  
  133.     }  
  134.   
  135. }  

在上述代碼中,關於contentType的獲取方式,我在上篇文章中已經講述過了,有興趣的可以看看。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM