简体   繁体   English

多部分表单请求引发:不存在必需的MultipartFile参数'image'

[英]Multipart form request throws: Required MultipartFile parameter 'image' is not present

I try to upload an image to a server using the postman. 我尝试使用邮递员将图像上传到服务器。 I am using spring to make the rest api as the followings: 我正在使用spring使其余api如下:

PostMan多部分请求

  @RequestMapping(value = "/uploadPrescription", method =RequestMethod.POST)
  public  ResponseEntity<ResponseSuccessData> uploadPatientPrescription(
        @RequestBody  @RequestParam("image") MultipartFile image)
        throws  IOException {

But it throws an error: 但这会引发错误:

      org.springframework.web.bind.MissingServletRequestParameterException: 
      Required MultipartFile parameter 'image' is not present

As you can see in the postman that key name is 'image' and in rest api is also @RequestParam("image"). 正如您在邮递员中看到的那样,键名是“ image”,其余api中的键名也是@RequestParam(“ image”)。

Setting value in content type - Content-type = multipart/form-data,boundaries='--abc' 在内容类型中设置值-Content-type = multipart / form-data,boundaries ='-abc'

This is my spring config for multipart - 这是我的多部分春季配置-

  @Bean
  public CommonsMultipartResolver multipartResolver() {

  CommonsMultipartResolver commonsMultipartResolver = new      CommonsMultipartResolver();
//commonsMultipartResolver.setMaxUploadSize(-1);
return commonsMultipartResolver;

}

What could be the problem? 可能是什么问题呢?

Please remove the header section 请删除标题部分

在此处输入图片说明

Please remove the - Content-Type : multipart/form-data;boundary='abc' setting in the header part of postman 请删除邮递员标题中的-Content-Type:multipart / form-data; boundary ='abc'设置

@RestController
public class UserOfferController {

// upload image 
    @RequestMapping(value = "/uploadimage", method = RequestMethod.POST)
    public ResponseEntity<ResponseObjectBean> uploadFile(@RequestParam("uploadedFile") MultipartFile file) {
        int statusCode;
        String msg;
        Object data = null;
        long maxsize = configuredValue.getFileMaxAcceptedSize();

        if (!file.isEmpty()) {

                String name = file.getOriginalFilename();

                String imagePath = "path to save your image ";


                try {
                    byte[] bytes = file.getBytes();
                    BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(imagePath)));
                    stream.write(bytes);
                    statusCode = 200;
                    msg = "DONE";
                    data = true;

                }  catch (Exception e) {
                    e.printStackTrace();
                    statusCode = 500;
                    msg = "FAIL";
                    data = false;

                }

        } else {
            statusCode = 500;
            msg = "FAIL";
            data = false;
        }
        responseData.setStatusCode(statusCode);
        responseData.setStatusMsg(msg);
        responseData.setData(data);
        return new ResponseEntity<ResponseObjectBean>(responseData, HttpStatus.OK);
    }

    }

Add these line in spring.xml 在spring.xml中添加这些行

    <!-- mutipart upload configuration -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- max upload size in bytes -->
        <property name="maxUploadSize" value="1024" />
        <!-- max size of file in memory (in bytes) -->
        <property name="maxInMemorySize" value="2048" />
    </bean>

在此处输入图片说明

In addition to Abhijit Chowdhury answer, if you are using spring security still you can remove Content-Type and just keep your token in the header, no need to remove everything from the header. 除了Abhijit Chowdhury的答案,如果您使用的是Spring Security,您仍然可以删除Content-Type并将令牌保留在标题中,而无需从标题中删除所有内容。

Also, it is important to restart postman. 另外,重新启动邮递员也很重要。

1.Remove header section in POSTMAN. 1.删​​除POSTMAN中的标题部分。

2.In your API: 2.在您的API中:

@RequestMapping(value = "/uploadPrescription", method =RequestMethod.POST)
      public  ResponseEntity<ResponseSuccessData> uploadPatientPrescription(
            @RequestBody  @RequestParam("image") MultipartFile image)
            throws  IOException {}

add the following: 添加以下内容:

consumes = MediaType.MULTIPART_FORM_DATA_VALUE

so it becomes: 变成:

@RequestMapping(value = "/uploadPrescription", method =RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public  ResponseEntity<ResponseSuccessData> uploadPatientPrescription(
        @RequestBody  @RequestParam("image") MultipartFile image)
        throws  IOException {}

Replace @RequestBody @RequestParam("image") to just @RequestBody("image") . @RequestBody @RequestParam("image")替换为@RequestBody("image") The first statement is not valid, see - Spring uploading files . 第一条语句无效,请参阅-Spring上载文件

暂无
暂无

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

相关问题 当我从Angular 4请求时,所需的MultipartFile参数&#39;file&#39;不存在 - Required MultipartFile parameter 'file' is not present when I request from Angular 4 在Spring 4.3.1中不存在必需的MultipartFile参数&#39;file&#39; - Required MultipartFile parameter 'file' is not present in spring 4.3.1 错误消息=必需的MultipartFile参数&#39;file&#39;不存在 - Error message = Required MultipartFile parameter 'file' is not present spring mvc 中不存在所需的 MultipartFile 参数“文件” - Required MultipartFile parameter 'file' is not present in spring mvc Postman - 所需的 MultipartFile 参数“文件”不存在 - Postman - Required MultipartFile parameter 'file' is not present 发布错误必需的MultipartFile []参数不存在 - post error Required MultipartFile [ ] parameter not present 所需的 MultipartFile 参数“文件”不存在 [Postman 和 Springboot] - Required MultipartFile parameter 'file' is not present [Postman and Springboot] 所需的 MultipartFile 参数“文件”不存在 - MultipartyEntityBuilder - Required MultipartFile parameter 'files' is not present - MultipartyEntityBuilder 处理程序执行导致异常:所需的MultipartFile参数&#39;file&#39;不存在 - Handler execution resulted in exception: Required MultipartFile parameter 'file' is not present 获取HTTP状态400 - 必需的MultipartFile参数&#39;file&#39;在spring中不存在 - Geting HTTP Status 400 - Required MultipartFile parameter 'file' is not present in spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM