Java使用HttpURLConnection上傳檔案
從普通Web頁面上傳檔案很簡單,只需要在form標籤叫上enctype="multipart/form-data"即可,剩餘工作便都交給瀏覽器去完成資料收集併發送Http請求。但是如果沒有頁面的話要怎麼上傳檔案呢?
由於脫離了瀏覽器的環境,我們就要自己去完成資料的收集併發送請求,所以就很麻煩了。首先我們來寫個JSP頁面並看看瀏覽器發出的Http請求是什麼樣的
JSP頁面:
<html> <head> <meta charset="UTF-8"> <title>TestSubmit</title> </head> <body> <form name="upform" action="upload.do" method="POST" enctype="multipart/form-data"> 引數<input type="text" name="username"/><br/> 檔案1<input type="file" name="file1"/><br/> 檔案2<input type="file" name="file2"/><br/> <input type="submit" value="Submit" /><br/> </form> </body> </html>
假如我引數寫的內容是hello word,然後二個檔案是二個簡單的txt檔案,form提交的資訊為:
-----------------------------7da2e536604c8 Content-Disposition: form-data; name="username" hello word -----------------------------7da2e536604c8 Content-Disposition: form-data; name="file1"; filename="D:/haha.txt" Content-Type: text/plain haha hahaha -----------------------------7da2e536604c8 Content-Disposition: form-data; name="file2"; filename="D:/huhu.txt" Content-Type: text/plain messi huhu -----------------------------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. 第十五、十六行就是檔案的內容了。如:
messi
huhu
10. 最後一行是-----------------------------7da2e536604c8--。注意最後多了二個“--”,作為結束的標誌。
那麼我們只要模擬這個資料,並寫入到Http請求中便能實現檔案的上傳。
其實,在我之前的文章:HttpClient使用詳解 ,就已經有利用HttpClient工具包上傳檔案的例子,HttpClient是Apache的一個強大的模擬併發送所有Http請求的開源類庫,有時間的,大家可以學習學習,但本篇文章中,並不以HttpClient為例,而是採用Java自帶的HttpURLConnection實現的。
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import net.sf.jmimemagic.Magic;
import net.sf.jmimemagic.MagicMatch;
public class HttpPostUploadUtil {
/**
* @param args
*/
public static void main(String[] args) {
String filepath = "E:\\ziliao\\0.jpg";
String urlStr = "http://127.0.0.1:8080/minicms/up/up_result.jsp";
Map<String, String> textMap = new HashMap<String, String>();
textMap.put("name", "testname");
Map<String, String> fileMap = new HashMap<String, String>();
fileMap.put("userfile", filepath);
String ret = formUpload(urlStr, textMap, fileMap);
System.out.println(ret);
}
/**
* 上傳圖片
* @param urlStr
* @param textMap
* @param fileMap
* @return
*/
public static String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap) {
String res = "";
HttpURLConnection conn = null;
String BOUNDARY = "---------------------------123821742118716"; //boundary就是request頭和上傳檔案內容的分隔符
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(conn.getOutputStream());
// text
if (textMap != null) {
StringBuffer strBuf = new StringBuffer();
Iterator<Map.Entry<String, String>> iter = textMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> entry = iter.next();
String inputName = (String) entry.getKey();
String inputValue = (String) entry.getValue();
if (inputValue == null) {
continue;
}
strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
strBuf.append(inputValue);
}
out.write(strBuf.toString().getBytes());
}
// file
if (fileMap != null) {
Iterator<Map.Entry<String, String>> iter = fileMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> entry = iter.next();
String inputName = (String) entry.getKey();
String inputValue = (String) entry.getValue();
if (inputValue == null) {
continue;
}
File file = new File(inputValue);
String filename = file.getName();
MagicMatch match = Magic.getMagicMatch(file, false, true);
String contentType = match.getMimeType();
StringBuffer strBuf = new StringBuffer();
strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" + filename + "\"\r\n");
strBuf.append("Content-Type:" + contentType + "\r\n\r\n");
out.write(strBuf.toString().getBytes());
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
}
}
byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
out.write(endData);
out.flush();
out.close();
// 讀取返回資料
StringBuffer strBuf = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
strBuf.append(line).append("\n");
}
res = strBuf.toString();
reader.close();
reader = null;
} catch (Exception e) {
System.out.println("傳送POST請求出錯。" + urlStr);
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
conn = null;
}
}
return res;
}
}
在上述程式碼中,關於contentType的獲取方式,我在上篇文章中已經講述過了,有興趣的可以看看。