【问题标题】:Multipart form request throws: Required MultipartFile parameter 'image' is not present多部分表单请求抛出:所需的 MultipartFile 参数“图像”不存在
【发布时间】:2016-12-25 14:44:12
【问题描述】:

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

  @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',而在rest api中也是@RequestParam("image")。

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

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

  @Bean
  public CommonsMultipartResolver multipartResolver() {

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

}

可能是什么问题?

【问题讨论】:

  • 但是,删除@RequestBody。它什么也没做。
  • 是的,你说得对,我已经通过删除 @RequestBody 进行了检查。但不行。

标签: java spring multipartform-data postman


【解决方案1】:
@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>

【讨论】:

  • 如果我删除了@RequestBody 注释,你的rest api 和我的rest 是一样的
  • 在 spring.xml 中添加给定的行。我相信它会起作用
  • 刚刚更新了问题,在spring中添加了multipart的配置代码,请看
  • Content-Type - multipart/form-data;boundary="--prescriptionImage"
  • 请删除标题并尝试它请告诉我它的工作与否
【解决方案2】:

除了 Abhijit Chowdhury 的回答之外,如果您仍然使用 spring security,您可以删除 Content-Type 并将您的令牌保留在标头中,无需从标头中删除所有内容。

另外,重启邮递员也很重要。

【讨论】:

    【解决方案3】:

    1.删除POSTMAN中的header部分。

    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】:

      请删除标题部分

      请删除 postman 标头部分的 - Content-Type : multipart/form-data;boundary='abc' 设置

      【讨论】:

        【解决方案5】:

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

        【讨论】:

        猜你喜欢
        • 2016-07-29
        • 2022-01-17
        • 2014-01-03
        • 1970-01-01
        • 2014-05-08
        • 2017-02-12
        • 1970-01-01
        • 1970-01-01
        • 2017-12-01
        相关资源
        最近更新 更多