【发布时间】: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