我有一个任务来形成这样的查询:
POST /foo HTTP/1.1
Content-Length: 68137
Content-Type: multipart/form-data; boundary=---------------------------974767299852498929531610575
-----------------------------974767299852498929531610575
Content-Disposition: form-data; name="description"
some text
-----------------------------974767299852498929531610575
Content-Disposition: form-data; name="myFile"; filename="1.txt"
Content-Type: text/plain
(content of the uploaded file 1.txt)
-----------------------------974767299852498929531610575--
这样它由两部分组成:来自文件的文本和数据。
为了形成这样的请求,我在客户端上执行以下代码:
public void sendPOSTRequest(String url, String authData, String attachmentFilePath, String outputFilePathName) {
String charset = "UTF-8";
File binaryFile = new File(attachmentFilePath);
String boundary = "------------------------" + Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
String CRLF = "\r\n"; // Line separator required by multipart/form-data.
int responseCode = 0;
try {
//Set POST general headers along with the boundary string (the seperator string of each part)
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
//connection.addRequestProperty("User-Agent", "CheckpaySrv/1.0.0");
//connection.addRequestProperty("Accept", "*/*");
//connection.addRequestProperty("Authentication", authData);
OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data;").append(CRLF);
writer.append("Content-Type: application/json").append(CRLF);// + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
writer.append("{\"a\":\"asda\"}").append(CRLF);// + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
writer.append(CRLF);
// Send binary file - part
// Part header
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: application/octet-stream").append(CRLF);// + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
writer.append(CRLF);
writer.flush();
// File data
Files.copy(binaryFile.toPath(), output);
output.flush();
// End of multipart/form-data.
writer.append(CRLF).append("--" + boundary + "--").flush();
//Отправить запрос и ожидать ответ от сервера
responseCode = ((HttpURLConnection) connection).getResponseCode();
if (responseCode != 200) //We operate only on HTTP code 200
{
return;
}
InputStream Instream = ((HttpURLConnection) connection).getInputStream();
// Write PDF file
BufferedInputStream BISin = new BufferedInputStream(Instream);
FileOutputStream FOSfile = new FileOutputStream(outputFilePathName);
BufferedOutputStream out = new BufferedOutputStream(FOSfile);
int i;
while ((i = BISin.read()) != -1) {
out.write(i);
}
// Cleanup
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
我尝试在服务器上使用以下代码处理此请求:
@RequestMapping(value = "/test6", method = RequestMethod.POST, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
public String test6(@RequestPart String part1) {
System.out.println("data1: "+part1);
System.out.println("Stop");
return "string1";
}
在向地址:http://localhost:8080/test6 发送 post 请求的过程中,在服务器上我收到以下消息:
WARN 2356 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver :
Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException:
Required request part 'part1' is not present]
告诉我如何使用 multipart/form-data 类型处理这样的 post 请求?
达到目的:发送一个需要修改文本+文件的请求,如下:
在执行请求的客户端代码中:
在处理请求的服务器代码中:
想法是绑定请求name="text"的部分,其中text连接到处理此请求的控制器的部分@RequestParam ("text") String text,我们对请求的第二部分执行此操作:name="file"其中文件 与@RequestParam("file") MultipartFile 文件 ModelMap modelMap 连接。
使用这种方法,您可以摆脱元素关联错误:Required request part ' text ' is not present或Required request part ' file ' is not present