Spring Boot 1.5: 在文件上传中接受 "multipart/*"
Spring Boot 1.5: accept "multipart/*" in file upload
我的 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
?
呃 - 这是一个错误。删除 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 客户端也适用。
我的 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
?
呃 - 这是一个错误。删除 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 客户端也适用。