【问题标题】:[org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present[org.springframework.web.multipart.support.MissingServletRequestPartException:所需的请求部分“文件”不存在
【发布时间】:2021-08-22 10:51:27
【问题描述】:

我正在将 .pdf 文件上传到 mongo DB,我正在使用 GridFS 模板。

文件控制器.java

@RequestMapping(value = "/upload", method = RequestMethod.POST, 
produces = MediaType.APPLICATION_JSON_VALUE)
 public ResponseEntity<?> 
 upload(@RequestParam("file")MultipartFile file) throws 
 IOException 
  {
    return new ResponseEntity<>(fileService.addFile(file), 
    HttpStatus.OK);
}

文件服务.java

public String addFile(MultipartFile upload) throws IOException {

    //define additional metadata
    DBObject metadata = new BasicDBObject();
    metadata.put("fileSize", upload.getSize());

    //store in database which returns the objectID
    Object fileID = template.store(upload.getInputStream(), upload.getOriginalFilename(), upload.getContentType(), metadata);

    //return as a string
    return fileID.toString();
}

编写控制器和服务方法后,我收到此错误:

[org.springframework.web.multipart.support.MissingServletRequestPartException: 所需的请求部分“文件”不存在]

谁能帮帮我,为什么我会得到这个异常?

谢谢

【问题讨论】:

    标签: mongodb spring-boot gridfs


    【解决方案1】:

    正如您在控制器中的服务器端提到的@RequestParam("file") MultipartFile file,因此您需要在从客户端上传文件时在表单数据中将密钥作为file 传递。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 2020-04-29
      • 2019-08-23
      相关资源
      最近更新 更多