我正在嘗試使用“multipart/form-data”發(fā)出 POST 請求,我需要發(fā)布一個文件(下面的代碼)和 4 個參數(shù)(名稱、類別...)所有字符串。我已經(jīng)可以使用下面的代碼發(fā)送文件,但不能使用參數(shù)。 // open a URL connection to the Servlet FileInputStream fileInputStream = new FileInputStream(sourceFile); URL url = new URL(upLoadServerUri); // Open a HTTP connection to the URL conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); // Allow Inputs conn.setDoOutput(true); // Allow Outputs conn.setUseCaches(false); // Don't use a Cached Copy conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); conn.setRequestProperty("fileToUpload", fileName); dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"fileToUpload\";filename=" + fileName + "" + lineEnd); dos.writeBytes(lineEnd); // create a buffer of maximum size bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // read file and write it into form... bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); }但是服務(wù)器從來不注冊參數(shù),我該如何解決這個問題?
1 回答

交互式愛情
TA貢獻1712條經(jīng)驗 獲得超3個贊
也許您應(yīng)該POST像 forrowing 演示代碼一樣構(gòu)建請求?希望能幫助到你。
### Send a form with the text and file fields
POST https://httpbin.org/post
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary
Content-Disposition: form-data; name="Name"
myName
--WebAppBoundary
Content-Disposition: form-data; name="category"
myCategory
--WebAppBoundary
Content-Disposition: form-data; name="data"; filename=".gitignore"
Content-Type: application/json
< ./.gitignore
--WebAppBoundary--
<> 2019-09-23T045805.200.json
###
添加回答
舉報
0/150
提交
取消