【发布时间】:2017-08-08 00:26:23
【问题描述】:
我的 Spring Boot 1.5.1 + Commons Fileupload 1.3.2 文件上传请求映射失败并显示 multipart/form-data。我的请求映射如下所示:
@RequestMapping(method=RequestMethod.POST, value="/api/users/uploads")
public @ResponseBody ResponseEntity<?> uploadUsers(
@RequestParam(value="file", required=true) MultipartFile file
) throws IOException {
String uploadFilename = file.getName();
try(InputStream input = file.getInputStream()) {
usersConverter.read(input);
}
return ResponseEntity.ok("Uploaded " + uploadFilename + " successfully.");
}
curl:
curl -v -u 'username:password'
-H "Content-Type: multipart/mixed"
-X POST -F file=@users.csv
http://localhost:8080/api/users/uploads
工作正常:
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
* Server auth using Basic with user 'username'
> POST /api/users/uploads HTTP/1.1
> Host: localhost:8080
> Authorization: Basic cGFzc3dvcmQ=
> User-Agent: curl/7.42.1
> Content-Length: 8237
> Expect: 100-continue
> Content-Type: multipart/mixed; boundary=-------------------4c6cae60d33268be
>
< HTTP/1.1 100 Continue
< HTTP/1.1 200
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Set-Cookie: JSESSIONID=8F80A63F1B83982DA1614ADD7BCB6C16;path=/;HttpOnly
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 27
< Date: Thu, 16 Mar 2017 17:39:51 GMT
<
* Connection #0 to host localhost left intact
但我有一些客户端发送multipart/form-data 而不是multipart/mixed,我希望这个请求映射能够同时处理这两个问题。我尝试将以下内容添加到@RequestMapping:
consumes="multipart/*"consumes={"multipart/mixed", "multipart/form-data"headers="content-type=multipart/*"headers={"content-type=multipart/mixed", "content-type=multipart/form-data"}
但在每种情况下:
curl -v -u 'username:password'
-H "Content-Type: multipart/form-data"
-X POST -F file=@users.csv
http://localhost:8080/api/users/uploads
以400 失败:
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
* Server auth using Basic with user 'username'
> POST /api/users/uploads HTTP/1.1
> Host: localhost:8080
> Authorization: Basic cGFzc3dvcmQ=
> User-Agent: curl/7.42.1
> Content-Length: 8237
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=-------------------85d8a22839d9bc08
>
< HTTP/1.1 100 Continue
< HTTP/1.1 400
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Set-Cookie: JSESSIONID=1EB31D866907E49F0972F601E22317D0;path=/;HttpOnly
< Content-Length: 0
< Date: Thu, 16 Mar 2017 17:42:00 GMT
< Connection: close
<
* Closing connection 0
我的 Angular 2.4 客户端发送 multipart/form-data 失败(可能相同)400。
任何想法如何让请求映射来处理multipart/form-data?
【问题讨论】:
-
尝试检查此链接spring upload file。也许你的卷曲是错误的。
-
@MatejMarconak --
curl命令似乎是正确的;它适用于mixed,但不适用于form-data——这是两者之间的唯一区别。在这两种情况下,curl都会添加所需的boundary值。我从非curl客户端得到相同的400,它提供form-data而不是mixed。 -
400: BAD REQUEST - 你可以尝试从 js 调用吗,因为 spring 定义看起来不错
-
查看我更新的问题,@MatejMarconak -- JS 客户端同样失败。
-
尝试将方法的参数更改为 - HttpServletRequest 请求、HttpServletResponse 响应,然后检查请求 ServletFileUpload.isMultipartContent(request) 的样子。
标签: java rest file-upload spring-boot apache-commons-fileupload