2017-10-17 1 views
0
를 통해 봄 REST 서비스에 파일을 업로드 할 수 없습니다

내가 매개 변수로 다중 파일을 받아 작은 봄 컨트롤러를 작성했습니다은 RestTemplate

@PutMapping("/fileUpload") 
    public String test(@RequestParam("test") MultipartFile file) { 
     System.out.println("In controller"); 
     return file.getOriginalFilename(); 
    } 

내가 사용하고 대응하는 RestTemplate은

Path path = Paths.get("C:\\Users\\Foo\\Desktop", "baz.txt"); 

      FormHttpMessageConverter messageConverter = new FormHttpMessageConverter(); 
      messageConverter.addPartConverter(new ByteArrayHttpMessageConverter()); 

      RestTemplate client = new RestTemplate(); 
      client.getMessageConverters().add(messageConverter); 

      String end_url = "http://localhost:8888/test/fileUpload"; 

      HttpHeaders headers = new HttpHeaders(); 
      headers.setContentType(MediaType.MULTIPART_FORM_DATA); 

      MultiValueMap<String, Object> body = new LinkedMultiValueMap<String, Object>(); 

      body.add("test", new ByteArrayResource(Files.readAllBytes(path))); 

      HttpEntity<MultiValueMap<String, Object>> entity 
       = new HttpEntity<MultiValueMap<String, Object>>(body, headers); 

      client.exchange(end_url, HttpMethod.PUT, entity, String.class, new HashMap()); 
입니다

에 상관없이 다음과 같은 오류가없는 킵 내가 무엇을하려고 :

Caused by: org.springframework.web.client.HttpClientErrorException: 400 null 
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:621) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:540) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.controller.Runner.run(Runner.java:57) [classes/:na] 
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    ... 6 common frames omitted 

내가 뭘 잘못된 ? ByteArrayHttpMessageConverter, ResourceHttpMessageConverter와 같은 모든 일반적인 메시지 변환기를 추가하려고 시도했으며 또한 commons-fileupload를 사용하여 시도했습니다.

누구나 내 RestTemplate에서 잘못된 일을 말할 수 있습니까?

+0

당신은 더 나은 정보를 서버 콘솔에서보고를 얻을 수 있습니다 수정하려고합니다. 나는'@ RequestParam' 대신에'@ RequestPart'가 더 적절하다고 생각합니다. – Vasan

+0

@Vasan No는'@ RequestParam'과 작동합니다 –

답변

0

안녕하세요? 당신의

@PutMapping("/fileUpload") 

@PutMapping(value = "/fileUpload", consumes = "multipart/form-data") 
+1

그것 없이는 작동했습니다. –