java Spring - RestTemplate - 多部分文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44154252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring - RestTemplate - Multipart File
提问by tomas
This controller works fine
这个控制器工作正常
@Controller
public class FileUploadController {
....
@PostMapping("/convert")
public void fileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes, HttpServletResponse response) {
Now i want to call this controller from another spring project via RestTemplate. I tried many things, but noting works. Here is my last code:
现在我想通过 RestTemplate 从另一个 spring 项目调用这个控制器。我尝试了很多东西,但注意是有效的。这是我最后的代码:
@Controller
public class FileController {
....
@PostMapping("/convert")
public void fileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes,
HttpServletResponse response) throws Exception {
ArrayList<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(
Arrays.asList(new FormHttpMessageConverter(),new MappingHymanson2HttpMessageConverter(), new ResourceHttpMessageConverter()));
RestTemplate template = restTemplate();
template.setMessageConverters(converters);
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
multipartRequest.add("file", file);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequest, header);
template.postForObject("http://localhost:8080/convert", requestEntity, String.class);
}
I if call FileUploadController (via postman) it works. If if call FileController i get this Exception
我如果调用 FileUploadController(通过邮递员)它就可以工作。如果调用 FileController 我得到这个异常
"exception":
"org.springframework.http.converter.HttpMessageNotWritableException",
"message": "Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile]",
"path": "/convert"
回答by andrearro88
Take a look at the answer here, it should be exactly what you are looking for: Attempting to test rest service with multipart file
看看这里的答案,它应该正是您要找的:尝试使用多部分文件测试休息服务
The issue there is about posting a multi-part file to a rest service using a RestTemplate
.
存在的问题是关于使用RestTemplate
.
Basically, what you have to do is to simulate a file upload. You can try something like this:
基本上,您要做的是模拟文件上传。你可以尝试这样的事情:
MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<String, Object>();
parameters.add("file", new FileSystemResource("file.jpg"));
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "multipart/form-data");
headers.set("Accept", "text/plain");
String result = restTemplate.postForObject(
"http://host:port/path",
new HttpEntity<MultiValueMap<String, Object>>(parameters, headers),
String.class);