java - MissingServletRequestPartException:所需的请求部分“文件”不存在 Springboot
问题描述
我有一个执行文件上传的控制器,我正在尝试从另一个服务向控制器端点发布请求。
@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
参数。您也可以在其中添加新属性(require
),@RequestParam
例如@RequestParam(value = "file", required = false)
. 属性的目的required
,使请求参数是必需的(如果为真)或可选的(如果为假)。
推荐阅读
- android - 如何使选项卡下的选择器滚动到选定的选项卡?
- excel - 将数据复制到一个工作表,其中有四行将每个单元格与原始工作表分开
- javascript - 用 Javascript 发推文的困难
- javascript - 如何在字符串文字中传递字符串数组?
- android - linux中的文件输入输出
- apache-kafka - Kafka:连接到节点 -1 身份验证失败,原因是:SSL 握手失败
- php - Laravel,如何根据此代码修改/编辑 excel?
- javascript - 如何将这两个确切的事件与不同的元素合并?
- php - 使用 POST 获取 PHP 将数据传递给 MS Access 的 HTML 表单
- php - 如何从mysql数据库创建图表