0

My Spring Boot 1.5.1 + Commons Fileupload 1.3.2 파일 업로드 요청 매핑이 multipart/form-data과 함께 실패합니다. 이 같은 내 요청 매핑 모습하십시오 curl스프링 부트 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."); 
} 

:

* 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/mixedmultipart/form-data을 보내 일부 클라이언트가 내가 좋겠 :

curl -v -u 'username:password' 
    -H "Content-Type: multipart/mixed" 
    -X POST -F [email protected] 
    http://localhost:8080/api/users/uploads 

잘 작동합니다 이 요청 매핑과 같이 두 가지를 모두 처리 할 수 ​​있습니다.

  • consumes="multipart/*"
  • consumes={"multipart/mixed", "multipart/form-data"
  • headers="content-type=multipart/*"
  • headers={"content-type=multipart/mixed", "content-type=multipart/form-data"}

을하지만, 각각의 경우에 : 나는 추가 해봤하여 @RequestMapping에 다음

curl -v -u 'username:password' 
    -H "Content-Type: multipart/form-data" 
    -X POST -F [email protected] 
    http://localhost:8080/api/users/uploads 

400 실패 : multipart/form-data를 전송

* 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 클라이언트는 (아마도 같은) 400와 함께 실패합니다.

어떻게하면 multipart/form-data을 처리 할 요청 매핑을 얻을 수 있습니까?

+0

[spring upload file] (https://www.mkyong.com/spring/curl-post-request-examples/) 링크를 확인해보십시오. 어쩌면 당신의 컬이 틀렸을 수도 있습니다. –

+0

@MatejMarconak - 'curl' 명령이 정확합니다. 그것은 '혼합'으로 작동하지만 '양식 데이터'로는 작동하지 않습니다. - 그 둘 사이의 유일한 차이점입니다. 그리고 두 경우 모두'curl'은 필요한'boundary' 값을 추가합니다. 그리고'mixed '대신에'form-data'를 제공하는 non-''curl' 클라이언트로부터'400'을 얻습니다. –

+0

400 : BAD REQUEST - 봄 정의가 괜찮아 보이므로 js에서 전화를 걸 수 있습니까? –

답변

0

어 - 그게 문제 야.

<!-- 
    <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 빈 정의를 제거 : commons-fileuploadcommons-io 종속성을 제거

/* 
    @Bean(name = "multipartResolver") 
    public CommonsMultipartResolver multipartResolver() { 
     CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); 
     multipartResolver.setMaxUploadSize(100000); 

     return multipartResolver; 
    } 
    */ 

는 요청 매핑은 multipart/mixed 예상대로 multipart/form-data 모두 작동 ... 내 각도 클라이언트도 작동합니다.