通常,对于文件上传,我使用了multipart / form-data,而且效果很好。但是,现在要求我的服务器能够接受文件application / octet-stream。
在服务器端,我有:
@ResponseBody
@RequestMapping(path = "/mock",
consumes = { MediaType.APPLICATION_OCTET_STREAM_VALUE },
method = RequestMethod.POST)
public ResponseEntity handleFileUpload(@RequestParam("file") MultipartFile file) {
return ResponseEntity.accepted().build();
}
而且我正在尝试使用curl进行测试:
curl -v -H "Content-Type:application/octet-stream" \
--data-binary @/home/user/Desktop/test.txt http://localhost:9090/mock
结果是拥有:
org.springframework.web.multipart.MultipartException: Current request is not a multipart request
我已经注意到我的curl命令中未指定“文件”部分,但预期在服务器端。目前尚不清楚下一步要移到何处以及什么是损坏的测试命令或服务器,或两者都有。
由于没有多部分表单数据消息,因此不能使用MultipartFile
。
您现在有两个可能性。
要么发送带有curl的多段消息:(看看名为file
的表单数据。)
curl -H "Content-Type: multipart/form-data" -F "file=@/home/user/Desktop/test.txt" http://localhost:9090/mock
或更改控制器:
@ResponseBody
@RequestMapping(path = "/mock",
consumes = { MediaType.APPLICATION_OCTET_STREAM_VALUE },
method = RequestMethod.POST)
public ResponseEntity handleFileUpload(final HttpServletRequest request) {
// request.getInputStream() will contain the content of the posted file
return ResponseEntity.accepted().build();
}
要以multipart/form-data
格式上传文件,请使用-F
或--form
curl -F "file=@/home/user/Desktop/test.txt" http://localhost:9090/mock
但是这样可能会将文件发送为Content-Type: text/plain
,而不是Content-Type: application/octet-stream
,就像我们在netcat服务器中看到的那样:
$ nc -l 9090
POST /mock HTTP/1.1
Host: 127.0.0.1:9090
User-Agent: curl/7.66.0
Accept: */*
Content-Length: 190
Content-Type: multipart/form-data; boundary=------------------------465160a3b9f7de24
--------------------------465160a3b9f7de24
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain
lol
--------------------------465160a3b9f7de24--
为了强制卷曲将其作为application/octet-stream
发送,请执行-F "file=@/home/user/Desktop/test.txt;type=application/octet-stream"
给我们:
$ nc -l 9090
POST /mock HTTP/1.1
Host: 127.0.0.1:9090
User-Agent: curl/7.66.0
Accept: */*
Content-Length: 204
Content-Type: multipart/form-data; boundary=------------------------444e61e5527966d1
--------------------------444e61e5527966d1
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: application/octet-stream
lol
--------------------------444e61e5527966d1--