這篇文章主要介紹“Java http調(diào)用接口提交表單以及文件的方法”,在日常操作中,相信很多人在Java http調(diào)用接口提交表單以及文件的方法問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Java http調(diào)用接口提交表單以及文件的方法”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)巴州,10年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):028-86922220
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è)回車(chē)換行 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"); // 提交文件基本信息(文件名、文件長(zhǎng)度、文件類(lèi)型等) 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++; } } // 文件寫(xiě)入結(jié)束 // 最后寫(xiě)入結(jié)束標(biāo)識(shí) 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)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
文章名稱:Javahttp調(diào)用接口提交表單以及文件的方法
URL分享:http://chinadenli.net/article36/pigcpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站制作、小程序開(kāi)發(fā)、網(wǎng)站內(nèi)鏈、商城網(wǎng)站、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)