简体   繁体   English

MissingServletRequestPartException:所需的请求部分“文件”不存在 Springboot

[英]MissingServletRequestPartException: Required request part 'file' is not present Springboot

I have a controller that performs file upload , i'm trying to post a request to controller endpoint from another service.我有一个执行文件上传的控制器,我正在尝试从另一个服务向控制器端点发布请求。

@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;
    }

Code from where i'm calling the above endpoint我调用上述端点的代码

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;

    }

I'm getting the below error, don't know the reason:我收到以下错误,不知道原因:

[MissingServletRequestPartException: Required request part 'file' is not present]

Been looking around for a while with no solution.找了半天没解决。

Probably, you are missing file param within your request, so you must consider file param inside of your request.可能您的请求中缺少file参数,因此您必须考虑请求中的file参数。 Also you can add new attribute ( require ) inside @RequestParam such as @RequestParam(value = "file", required = false) .您也可以在@RequestParam添加新属性( require ),例如@RequestParam(value = "file", required = false) Purpose of required attribute, to make request parameter to be required (if true) or optional (if false). required属性的目的,使请求参数成为必需(如果为真)或可选(如果为假)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring org.springframework.web.multipart.support.MissingServletRequestPartException,所需的请求部分“文件”不存在 - Spring org.springframework.web.multipart.support.MissingServletRequestPartException, Required request part 'file' is not present 上传文件springboot所需的请求部分“文件”不存在 - upload file springboot Required request part 'file' is not present [org.springframework.web.multipart.support.MissingServletRequestPartException:所需的请求部分“图像”不存在] - [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'image' is not present] Tomcat:所需的请求部分“文件”不存在 - Tomcat : Required request part 'file' is not present 出现“不存在所需的请求部分&#39;文件&#39;”错误 - Getting “Required request part 'file' is not present” error Spring Boot Required 请求部分“文件”不存在 - Spring Boot Required request part 'file' is not present Spring 文件上传 - “所需的请求部分不存在” - Spring File Upload - 'Required request part is not present' Spring Thymeleaf所需的请求部分“文件”不存在 - Spring Thymeleaf Required request part 'file' is not present 所需的要求部分“照片”不存在 - Required request part 'photo' is not present 此请求部分“文件”不存在 - This request part 'file' is not present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM