被调用方代码
@PostMapping("/certificateUpload")
public Result<?> certificateUpload(@RequestPart("file") MultipartFile file,
@RequestParam String certificateType) {
return certificateService.certificateUpload(file, certificateType);
}
调用方代码
public Result<?> certificateUpload(MultipartFile file, String certificateType) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
try {
map.add("file",file.getResource());
map.add("certificateType",certificateType);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
ResponseEntity<Result> response = restTemplate.exchange(certificateUploadUrl, HttpMethod.POST, requestEntity, Result.class);
return response.getBody();
} catch (IOException e) {
e.printStackTrace();
}
return Result.ok("上传失败");
}
注意:百度有的例子是获取到MultipartFile的InputStream流包装在InputStreamResource中放在map里。
实际上使用时被调用方会报错 Required request part 'file' is not present 。接收不到传过去的file
发现点进getResource()源码看注释就已经写的很清楚了
Return a Resource representation of this MultipartFile.This can be used as input to the RestTemplate or the WebClient to expose content length and the filename along with the InputStream.
Returns:this MultipartFile adapted to the Resource contract
Since:5.1标红部分说明可以用于RestTemplate或WebClient
- MultipartFile RestTemplate 类型 数据 Springmultipartfile resttemplate类型 数据 resttemplate springcloud spring ribbon multivaluemap resttemplate spring resttemplate spring resttemplate模板spring rest resttemplate spring feign 示例resttemplate spring boot resttemplate restclient特性spring 小结resttemplate spring resttemplate spring https boot