java文件上傳報文解析與實現
- 2019 年 10 月 30 日
- 筆記
版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/luo4105/article/details/75561101
文件上傳用web實現很容易,只需要在form加上enctype="multipart/form-data"就可以。
用java實現也很簡單,注意一下請求報文格式就好了。
首先我們看一下報文數據
POST /epg/admin/epg/originalfile/upload.doHTTP/1.1 Accept-Encoding: gzip, deflate Content-Length: 3714479 Content-Type: multipart/form-data;boundary=---------------------------161393124728552 Cookie:JSESSIONID=892218FFF1CD71AA8A4A2E8A52FA2BBA Host: 192.168.1.107:8090 Connection: keep-alive -----------------------------161393124728552 Content-Disposition: form-data;name="file"; filename="17df-x0hjki.xml" Content-Type: text/xml <?xml version="1.0"encoding="UTF-8"?><tv> /** 文件數據 */</tv> -----------------------------161393124728552 Content-Disposition: form-data;name="sourceId" 2 -----------------------------161393124728552--
POST /epg/admin/epg/originalfile/upload.doHTTP/1.1
代表這是post提交的
Content-Type: multipart/form-data;boundary=---------------------------161393124728552 Cookie:JSESSIONID=892218FFF1CD71AA8A4A2E8A52FA2BBA Connection: keep-alive
這三個是需要手動自己添加設置的,第一句是代表類型是文件,boundary是參數的分隔符,使用時在前面加上」–」。
第二行是登陸驗證需要的。
第三行是保持連接的。
-----------------------------161393124728552 Content-Disposition: form-data;name="file"; filename="17df-x0hjki.xml" Content-Type: text/xml <?xml version="1.0"encoding="UTF-8"?><tv> /** 文件數據 */</tv> -----------------------------161393124728552 Content-Disposition: form-data;name="sourceId" 2 -----------------------------161393124728552--
這是數據報文
第一行是」—「+ boundary分隔符,
第二行是文件的名稱,後台接受的參數名
第三行是文件類型
第四行是空格,代碼中是」n」
第五行開始是文件內容
第二部分是表單參數的值
最後注意一下結尾,是」–」+boundary+」—「
這裡用HttpConnection實現
public static void uploadFile(String fileName) { try { // 換行符 final StringnewLine = "rn"; final String boundaryPrefix = "--"; // 定義數據分隔線 String BOUNDARY = "----WebKitFormBoundaryAvVfvFPWnCBmJzQ5"; // 服務器的域名 URL url = new URL("http://192.168.1.107:8090/epg/admin/epg/originalfile/upload.do"); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); // 設置為POST情 conn.setRequestMethod("POST"); // 發送POST請求必須設置如下兩行 conn.setDoOutput(true); conn.setDoInput(true); // 設置請求頭參數 conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + BOUNDARY); conn.setRequestProperty("Content-Length", "3714479"); conn.setRequestProperty("Cookie", "JSESSIONID=CEA219E45CC9894D59D9F07FBD8597C6"); conn.setRequestProperty("User-Agent", "Mozilla/5.0(Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/59.0.3071.115 Safari/537.36"); OutputStream out = new DataOutputStream(conn.getOutputStream()); StringBuilder sb2 = new StringBuilder(); sb2.append(boundaryPrefix + BOUNDARY + newLine); // 文件參數,photo參數名可以隨意修改 sb2.append("Content-Disposition:form-data; name="sourceId""); // 參數頭設置完以後需要兩個換行,然後才是參數內容 sb2.append(newLine); sb2.append(newLine); sb2.append(2); // 將參數頭的數據寫入到輸出流中 out.write(sb2.toString().getBytes()); // 上傳文件 File file = new File(fileName); StringBuilder sb = new StringBuilder(); sb.append(newLine + boundaryPrefix + BOUNDARY + newLine); // 文件參數,參數名可以隨意修改 sb.append("Content-Disposition:form-data; name="file";filename="" + file.getName() + """); sb.append(newLine + "Content-Type: text/xml"); // 參數頭設置完以後需要兩個換行,然後才是參數內容 sb.append(newLine); sb.append(newLine); out.write(sb.toString().getBytes()); // 數據輸入流,用於讀取文件數據 DataInputStream in = new DataInputStream(new FileInputStream( file)); byte[] bufferOut = new byte[1024]; int bytes = 0; // 每次讀1KB數據,並且將文件數據寫入到輸出流中 while ((bytes= in.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } // 最後添加換行 in.close(); System.out.println(sb.toString()); System.out.println(sb2.toString()); // 定義最後數據分隔線,即--加上BOUNDARY再加上--。 byte[]end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix + newLine) .getBytes(); // 寫上結尾標識 out.write(end_data); out.flush(); out.close(); // 定義BufferedReader輸入流來讀取URL的響應 BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (Exceptione) { System.out.println("發送POST請求出現異常!"+ e); e.printStackTrace(); } }