我使用HttpURLConnection通过post方法发送文件。我用文件发送一个参数,这是'student_id'。当在每个post请求中发送一个文件时,代码工作正常。但是,我如何更新下面的代码在一个post请求中发送多个文件,其中所有文件都属于相同的'student_id'?
try{
File textFile2 = new File("info.txt");
URL url = new URL("htttp://wwww.students.com");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setChunkedStreamingMode(0);
urlConnection.setDoOutput(true);
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
OutputStream output = urlConnection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name="student_id"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF).append("25").append(CRLF).flush();
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name="newfile"; filename="" + textFile2.getName() + """).append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!
writer.append(CRLF).flush();
Files.copy(textFile2.toPath(), output);//copies all bytes in a file to the output stream
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush();
writer.append("--" + boundary + "--").append(CRLF).flush();
InputStream responseStream;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我试图在'newfile'中添加'multiple'参数,但它不起作用
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name="newfile" multiple; filename="" + textFile2.getName() + """).append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!
writer.append(CRLF).flush();
Files.copy(textFile2.toPath(), output);//copies all bytes in a file to the output stream
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush();
writer.append("--" + boundary + "--").append(CRLF).flush();
似乎您正试图发布具有1 表单字段的multipart/form-data
请求与student_id
的name
参数和多个文件部分来上传文件。
您可以通过在单独的部分中提供每个文件来发送多个文件,但所有文件都具有相同的name
参数。
例如,上传textFile1
的文件,发送第一个文件部分, newfile
参数name
:
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name="newfile"; filename="" + textFile1.getName()+ """).append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF);
writer.flush();
FileInputStream inputStream = new FileInputStream(textFile1);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
writer.append(CRLF);
writer.flush();
然后,通过发送newfile
的相同name
参数的文件部分,可以上传textFile2
的另一个文件:
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name="newfile"; filename="" + textFile2.getName()+ """).append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF);
writer.flush();
FileInputStream inputStream = new FileInputStream(textFile2);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
writer.append(CRLF);
writer.flush();
正如您所看到的,除了要上传的文件之外,代码几乎是相同的。建议将代码放入一个方法中并调用它来发送每个文件部分。