0

My Spring Boot 1.5.1 + Commons Fileupload 1.3.2文件上傳請求映射失敗,multipart/form-data。我請求映射是這樣的:Spring Boot 1.5:接受文件上傳中的「multipart/*」

@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 [email protected] 
    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 [email protected] 
    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 

我的角2.4客戶機,它發送multipart/form-data失敗(大概相同)400

任何想法如何得到請求映射來處理multipart/form-data

+0

請嘗試檢查此鏈接[spring上傳文件](https://www.mkyong.com/spring/curl-post-request-examples/)。也許你的捲曲是錯誤的。 –

+0

@MatejMarconak - 'curl'命令看起來是正確的;它使用'mixed'而不是'form-data' - 這是兩者之間的唯一區別。在這兩種情況下,curl都會添加所需的邊界值。而且我從'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' –

+0

400:壞請求 - 你可以嘗試從js調用,因爲彈簧定義看起來不錯 –

回答

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預期......我的角客戶端的工作了。