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

时间:2016-08-18 15:27:35

标签: java spring multipartform-data postman

我尝试使用邮递员将图像上传到服务器。我使用spring来制作其余的api如下:

PostMan mutlipart request

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

但它引发了一个错误:

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

正如你在邮递员中看到的那样,关键名称是'image',而在api中,api也是@RequestParam(“image”)。

在内容类型中设置值 - Content-type = multipart / form-data,boundary =' - abc'

这是我的多部分弹簧配置 -

  @Bean
  public CommonsMultipartResolver multipartResolver() {

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

}

可能是什么问题?

5 个答案:

答案 0 :(得分:3)

请删除标题部分

enter image description here

请删除 - Content-Type:multipart / form-data; boundary =&#39; abc&#39;在邮递员的标题部分设置

答案 1 :(得分:2)

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

    }

在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>

enter image description here

答案 2 :(得分:1)

除了Abhijit Chowdhury的答案之外,如果您仍然使用Spring安全性,则可以删除Content-Type并将标记保留在标题中,无需从标题中删除所有内容。

此外,重新开始邮递员很重要。

答案 3 :(得分:0)

1.删除POSTMAN中的标题部分。

2.在您的API中:

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

添加以下内容:

consumes = MediaType.MULTIPART_FORM_DATA_VALUE

所以它变成了:

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

答案 4 :(得分:-1)

@RequestBody @RequestParam("image")替换为@RequestBody("image")。 第一个语句无效,请参阅 - Spring uploading files