我有一个执行文件上传的控制器,我正在尝试从另一个服务向控制器端点发送请求。
@RequestMapping(path = "/upload/{id}", method = RequestMethod.POST)
public String uploadBaseImage(@RequestParam("data") String imageData, @RequestParam("file") MultipartFile file,@PathVariable("id") String id)
throws Exception {
String imageUrl = feedHandler.UploadAndSetImageUrl(imageData, file,Integer.parseInt(id));
return imageUrl;
}
我呼叫上述端点的代码
public SupplierFeedResponse uploadBaseFeedImage(String data, MultipartFile file, String supplierId) throws IOException {
String uploadBaseFeedEndpoint = uploadFeedEndpoint+Constants.FEED_SERVICE_BASE_FEED_UPLOAD_URI+supplierId;
SupplierFeedResponse supplierFeedResponse = new SupplierFeedResponse();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", new ByteArrayResource(file.getBytes()));
body.add("data", data);
log.info("Request body : "+body.toString());
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(uploadBaseFeedEndpoint, requestEntity, String.class);
return supplierFeedResponse;
}
我收到以下错误,不知道原因:
[MissingServletRequestPartException: Required request part 'file' is not present]
过去四处寻找解决方法。
可能您的请求中缺少“ file”参数,因此您必须在请求中考虑“ file”参数。您也可以在@RequestParam中添加新属性(required),例如@ RequestParam(value =“ file”,required = false)。 [required”属性的目的,是使请求参数为必需或可选。