更新时间:2022-01-28 07:34:30
似乎您正在尝试使用 1 表单字段 发布 multipart/form-data
请求student_id
的name
参数和多个文件部分上传文件.
Seems that you are trying to post a multipart/form-data
request with 1 form field with name
parameter of student_id
and multiple file part to upload files.
您可以通过在单独的部分中提供每个文件来发送多个文件,但所有文件都具有相同的 name
参数.
You can send multiple filesby supplying each file in a separate part but all with the same name
parameter.
例如,您可以通过发送带有 newfile
name 参数的第一个 file part 来上传 textFile1
的文件代码>:
For example, you can upload a file of textFile1
by sending the first file part with name
parameter of newfile
:
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
的另一个文件:
Then, you can upload another file of textFile2
by sending file part with same name
parameter of newfile
:
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();
如您所见,除了要上传的文件之外,代码几乎相同.建议将代码放入一个方法中并调用它来发送每个文件部分.
As you can see, the code are almost the same except the file to upload. It is recommend to put the code into a method and call it to send each file part.