Spring Boot 1.5:接受" multipart / *"在文件上传

时间:2017-03-16 17:15:58

标签: java rest file-upload spring-boot apache-commons-fileupload

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的请求映射的?

1 个答案:

答案 0 :(得分:0)

哎呀 - 这是一个错误。删除commons-fileuploadcommons-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/mixedmultipart/form-data ......我的Angular客户端也可以工作。