Java Spring-restemplate-Multipart文件
这个控制器工作正常Java Spring-restemplate-Multipart文件,java,spring,spring-mvc,multipart,Java,Spring,Spring Mvc,Multipart,这个控制器工作正常 @Controller public class FileUploadController { .... @PostMapping("/convert") public void fileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes, HttpServletResponse response) { 现在我想通过RestTemplate从
@Controller
public class FileUploadController {
....
@PostMapping("/convert")
public void fileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes, HttpServletResponse response) {
现在我想通过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 MappingJackson2HttpMessageConverter(), 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);
}
看看这里的答案,它应该正是你想要的: 这里的问题是关于使用
restemplate
将多部分文件发布到rest服务
基本上,您需要做的是模拟文件上传。您可以尝试以下方法:
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);
MultiValueMap参数=新链接的MultiValueMap();
parameters.add(“file”,newfilesystemresource(“file.jpg”);
HttpHeaders=新的HttpHeaders();
headers.set(“内容类型”、“多部分/表单数据”);
标题。设置(“接受”、“文本/普通”);
字符串结果=restTemplate.postForObject(
"http://host:port/path",
新的HttpEntity(参数、标头),
字符串(类);
看看这里的答案,它应该正是您想要的:
这里的问题是关于使用restemplate
将多部分文件发布到rest服务
基本上,您需要做的是模拟文件上传。您可以尝试以下方法:
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);
MultiValueMap参数=新链接的MultiValueMap();
parameters.add(“file”,newfilesystemresource(“file.jpg”);
HttpHeaders=新的HttpHeaders();
headers.set(“内容类型”、“多部分/表单数据”);
标题。设置(“接受”、“文本/普通”);
字符串结果=restTemplate.postForObject(
"http://host:port/path",
新的HttpEntity(参数、标头),
字符串(类);
您能否提供此问题与您发布的问题之间的关系。参考社区guidelines@harshavmb完成“我的代码”的唯一区别是参数。添加(“文件”,新文件系统资源(“file.jpg”);”。而不是“multipartRequest.add(“file”,file);”。如果我以这种方式更改代码,请求将传递到第一个控制器。伟大的但是现在我变成了一个关于响应“无法提取响应:没有为响应类型[class java.lang.String]找到合适的HttpMessageConverter”的例外……可能是您的客户端无法理解服务器接收到的响应类型。由于您期望的响应是一个简单的字符串,请尝试将响应类型添加到@PostMapping
符号中,如下所示:@PostMapping(path=“/convert”,products=MediaType.TEXT\u PLAIN\u VALUE)
您还可以提供此问题与您发布的问题的关系吗。参考社区guidelines@harshavmb完成“我的代码”的唯一区别是参数。添加(“文件”,新文件系统资源(“file.jpg”);”。而不是“multipartRequest.add(“file”,file);”。如果我以这种方式更改代码,请求将传递到第一个控制器。伟大的但是现在我变成了一个关于响应“无法提取响应:没有为响应类型[class java.lang.String]找到合适的HttpMessageConverter”的例外……可能是您的客户端无法理解服务器接收到的响应类型。由于您期望的响应是一个简单字符串,请尝试将响应类型添加到@PostMapping
符号中,如下所示:@PostMapping(path=“/convert”,products=MediaType.TEXT\u PLAIN\u VALUE)