通常,对于文件上传,我使用了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命令中未指定“文件”部分,但预期在服务器端。尚不清楚下一步要移到何处以及什么是损坏的测试命令或服务器,或两者都没有。
答案 0 :(得分:0)
由于没有多部分表单数据消息,因此不能使用MultipartFile
。
您现在有两种可能性。
发送带有curl的多段消息:
(看看名为file
的表单数据。)
curl -F "file=@/home/user/Desktop/test.txt;type=application/octet-stream" 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();
}
答案 1 :(得分:0)
要以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--
为了强制curl以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--