您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java http調(diào)用接口提交表單以及文件的方法”,在日常操作中,相信很多人在Java http調(diào)用接口提交表單以及文件的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java http調(diào)用接口提交表單以及文件的方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
Java HttpURLConnection 使用
/** * */ package com.demo.java; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; /** * <p>描述:</p> * * @author saohuo * @date 2019年11月4日 * @version */ public class MultipartFormDataPost { // multipart/form-data; 表單各字段提交分隔符 private static String boundary = "--69695201314"; public void submit(Map<String, String> generalField, List<File> files) throws Exception { HttpURLConnection connection = null; OutputStream os = null; URL url = new URL("http:"); connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Accept-Charset", "UTF-8"); connection.setRequestProperty("content-type", "multipart/form-data;boundary="+boundary); os = new DataOutputStream(connection.getOutputStream()); // 文本參數(shù)組裝 StringBuffer textFormBody = new StringBuffer(); Map<String, String> formField = generalField; formField.forEach((k,v) -> { textFormBody.append("\r\n").append("--").append(boundary).append("\r\n"); textFormBody.append("Content-Disposition: form-data; name=\""+k+"\""); textFormBody.append("\r\n\r\n");//名稱與數(shù)據(jù)之間要有兩個(gè)回車換行 textFormBody.append(v); }); // 提交文本表單 os.write(textFormBody.toString().getBytes("UTF-8")); // 文件數(shù)據(jù) if (files != null && !files.isEmpty()) { int fileIndex = 0; for(File file : files) { String filename = file.getName(); String fileMinetype = URLConnection.getFileNameMap().getContentTypeFor(filename); StringBuffer fileFormBody = new StringBuffer(); fileFormBody.append("\r\n").append("--").append(boundary).append("\r\n"); fileFormBody.append("Content-Disposition: form-data; name=\"files["+fileIndex+"]\"; filename=\"" + filename + "\""); fileFormBody.append("\r\n"); fileFormBody.append("Content-Type:" + fileMinetype); fileFormBody.append("\r\n\r\n"); // 提交文件基本信息(文件名、文件長度、文件類型等) os.write(fileFormBody.toString().getBytes()); InputStream fileStream = new FileInputStream(file); DataInputStream fileDataIs = new DataInputStream(fileStream); int filebytes = 0; byte[] filebufferOut = new byte[1024]; while ((filebytes = fileDataIs.read(filebufferOut)) != -1) { os.write(filebufferOut, 0, filebytes); } fileDataIs.close(); fileIndex++; } } // 文件寫入結(jié)束 // 最后寫入結(jié)束標(biāo)識 byte[] endBodyData = ("\r\n--" + boundary + "--\r\n").getBytes(); os.write(endBodyData); os.flush(); int responseCode = connection.getResponseCode(); // 提交結(jié)果狀態(tài)碼 System.out.println(responseCode); } }
到此,關(guān)于“Java http調(diào)用接口提交表單以及文件的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。