Spring boot 所需请求部分';文件';不存在]springboot客户端

Spring boot 所需请求部分';文件';不存在]springboot客户端,spring-boot,post,http-request-parameters,Spring Boot,Post,Http Request Parameters,我在客户端中有以下代码: RestTemplate restTemplate = new RestTemplate(); File file = new File("C:\\temp\\aadocejem.doc"); MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>(); map.add(

我在客户端中有以下代码:

          RestTemplate restTemplate = new RestTemplate();
          File file = new File("C:\\temp\\aadocejem.doc");
          MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();

          map.add("file", file);


          String result = restTemplate.postForObject(url+"/doc_file", map, String.class);
RestTemplate RestTemplate=new RestTemplate();
File File=新文件(“C:\\temp\\aadocejem.doc”);
MultiValueMap=新链接的MultiValueMap();
添加(“文件”,文件);
String result=restemplate.postForObject(url+“/doc_file”,map,String.class);
这个代码就是你所说的上面的代码:

 @PostMapping("/doc_file")
    public ResponseEntity<File> docFileV1(
        @RequestParam("file") MultipartFile originalDocFile) {

        return ResponseEntity.ok(docFileService.processDocFile(originalDocFile));

    }
@PostMapping(/doc\u文件)
公共响应文件v1(
@RequestParam(“文件”)多部分文件(原始文件){
返回ResponseEntity.ok(docFileService.processDocFile(originalDocFile));
}
它在服务器上给我的错误:已解决[org.springframework.web.multipart.support.MissingServletRequestPartException:所需的请求部分“文件”不存在]


它在客户端中给我的错误是:org.springframework.web.client.HttpClientErrorException$BadRequest:400:[{“时间戳”:“2020-04-23T10:55:32.258+0000”,“状态”:400,“错误”:“错误请求”,“消息”:“所需的请求部分‘文件’不存在”,“跟踪”:“org.springframework.web.multipart.support.MissingServlet…”(5758字节)]

这不适用于postForObject

改用postForEntity:

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

MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", new FileSystemResource(file));

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

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url+"/doc_file", requestEntity, String.class);
HttpHeaders=newhttpheaders();
headers.setContentType(MediaType.MULTIPART\uuuuuu FORM\uuuuu数据);
MultiValueMap body=新链接的MultiValueMap();
添加(“文件”,新文件系统资源(文件));
HttpEntity requestEntity=新的HttpEntity(主体、标题);
RestTemplate RestTemplate=新RestTemplate();
ResponseEntity response=restemplate.postForEntity(url+“/doc\u file”,requestEntity,String.class);

这不适用于postForObject

改用postForEntity:

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

MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", new FileSystemResource(file));

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

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url+"/doc_file", requestEntity, String.class);
HttpHeaders=newhttpheaders();
headers.setContentType(MediaType.MULTIPART\uuuuuu FORM\uuuuu数据);
MultiValueMap body=新链接的MultiValueMap();
添加(“文件”,新文件系统资源(文件));
HttpEntity requestEntity=新的HttpEntity(主体、标题);
RestTemplate RestTemplate=新RestTemplate();
ResponseEntity response=restemplate.postForEntity(url+“/doc\u file”,requestEntity,String.class);

我更改了它,但它一直给我相同的错误:无法解析public org.springframework.http.ResponseEntity com.n.poc.controller.PocController.docFileV1(org.springframework.web.multipart.MultipartFile)中的参数[0]:所需的请求部分“文件”不存在。您可能必须将该文件包含在Resource.body.add(“文件”)中“,新的FileSystemResource(文件));我更新了答案我更改了答案,但它一直给我相同的错误:无法解析public org.springframework.http.ResponseEntity com.n.poc.controller.PocController.docFileV1(org.springframework.web.multipart.MultipartFile)中的参数[0]:所需的请求部分“文件”不存在。您可能必须将该文件包含在Resource.body.add(“file”,new FileSystemResource(file))中;我已更新了答案