【发布时间】:2016-02-15 12:32:22
【问题描述】:
我正在使用 RestTemplate 客户端代码并希望在服务器端访问图像。我想在 Tomcat 的自定义目录上上传图片,但出现错误:
Caused by: org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.apache.http.entity.mime.MultipartEntity] and content type [multipart/form-data]
RestTemplate 客户端代码:
public String saveCompanylogo(File file){
String url = COMPANY_URL+ "/saveCompanyLogo";
MultipartEntity multiPartEntity = new MultipartEntity ();
FileBody fileBody = new FileBody(file) ;
//Prepare payload
multiPartEntity.addPart("file", fileBody) ;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultipartEntity> entity = new HttpEntity<MultipartEntity> (multiPartEntity, headers);
ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, new ParameterizedTypeReference<String>() {
});
return exchange.getBody();
}
我的服务器端(控制器)代码是:
@RequestMapping(method = POST, value = "/saveCompanyLogo")
@Consumes("multipart/form-data")
public String saveCompanylogo(@RequestParam("file") MultipartFile file) {
System.out.println(""+file);
//Todo coding
return "stringData";
}
【问题讨论】:
-
我使用
FileSystemResource而不是FileBody。服务器收到InputStream -
这对我来说不起作用。你能附上你的代码吗?
标签: java spring-mvc resttemplate