被调用方代码
@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
标签:map,Spring,RestTemplate,certificateUpload,file,certificateType,MultipartFile From: https://www.cnblogs.com/qzzzz/p/17462481.html发现点进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