利用HttpURLConnection發送post請求上傳多個文件
本文要用java.net.HttpURLConnection來實現多個文件上傳
1. 研究 form 表單到底封裝了什麼樣的信息發送到servlet。
假如我參數寫的內容是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.第一行是“ -----------------------------7d92221b604bc ”作為分隔符,然後是“ /r/n ” 回車換行符。 這個7d92221b604bc 分隔符瀏覽器是隨機生成的。
2.第二行是Content-Disposition: form-data; name="file2"; filename="D:/huhu.txt";name=對應input的name值,filename對應要上傳的文件名(包括路徑在內),
3.第三行如果是文件就有Content-Type: text/plain;這裏上傳的是txt文件所以是text/plain,如果上穿的是jpg圖片的話就是image/jpg了,可以自己試試看看。
然後就是回車換行符。
4.在下就是文件或參數的內容或值了。如:hello word。
5.最後一行是-----------------------------7da2e536604c8--,注意最後多了二個--;
有了這些就可以使用HttpURLConnection來實現上傳文件功能了
- private void upload(String[] uploadFiles, String actionUrl) {
- String end = "/r/n";
- String twoHyphens = "--";
- String boundary = "*****";
- try {
- URL url = new URL(actionUrl);
- HttpURLConnection con = (HttpURLConnection) url.openConnection();
- // 發送POST請求必須設置如下兩行
- con.setDoInput(true);
- con.setDoOutput(true);
- con.setUseCaches(false);
- con.setRequestMethod("POST");
- con.setRequestProperty("Connection", "Keep-Alive");
- con.setRequestProperty("Charset", "UTF-8");
- con.setRequestProperty("Content-Type",
- "multipart/form-data;boundary=" + boundary);
- DataOutputStream ds =
- new DataOutputStream(con.getOutputStream());
- for (int i = 0; i < uploadFiles.length; i++) {
- String uploadFile = uploadFiles[i];
- String filename = uploadFile.substring(uploadFile.lastIndexOf("//") + 1);
- ds.writeBytes(twoHyphens + boundary + end);
- ds.writeBytes("Content-Disposition: form-data; " +
- "name=/"file" + i + "/";filename=/"" +
- filename + "/"" + end);
- ds.writeBytes(end);
- FileInputStream fStream = new FileInputStream(uploadFile);
- int bufferSize = 1024;
- byte[] buffer = new byte[bufferSize];
- int length = -1;
- while ((length = fStream.read(buffer)) != -1) {
- ds.write(buffer, 0, length);
- }
- ds.writeBytes(end);
- /* close streams */
- fStream.close();
- }
- ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
- ds.flush();
- // 定義BufferedReader輸入流來讀取URL的響應
- InputStream is = con.getInputStream();
- int ch;
- StringBuffer b = new StringBuffer();
- while ((ch = is.read()) != -1) {
- b.append((char) ch);
- }
- String s = b.toString();
- if (s.contains("successfully")) {
- // for (int i = 1; i < 5; i++) {
- int beginIndex = s.indexOf("url =") + 5;
- int endIndex = s.indexOf("/n", beginIndex);
- String urlStr = s.substring(beginIndex, endIndex).trim();
- System.out.println(urlStr);
- // }
- }
- ds.close();
- } catch (Exception e) {
- }
- }
最後更新:2017-04-04 07:04:13