My 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
发送multipart/form-data
的我的Angular 2.4客户端失败了(可能是相同的)400
。
我是如何获得处理multipart/form-data
的请求映射的?
答案 0 :(得分:0)
commons-fileupload
和commons-io
依赖项:
<!--
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
-->
并删除multipartResolver
bean定义:
/*
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(100000);
return multipartResolver;
}
*/
然后请求映射按预期适用于multipart/mixed
和multipart/form-data
......我的Angular客户端也可以工作。