spring - RequestBody 传递的 MultiparFile 为空
问题描述
我正在尝试在 Post 请求中传递文件列表,如下所示:
邮差:
控制器:
@ApiOperation(value = "Upload documents in the GED")
@PostMapping(consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
@ResponseStatus(code = HttpStatus.CREATED)
public void uploadFileHandler(@ModelAttribute DocumentsUploadForm documentsUploadForm, @RequestBody List<MultipartFile> documents,
@RequestParam("easy") String easy) throws BusinessException, IOException, CrsPermissionViolationException {
....
....
}
所有传递的参数和变量都不为空,只有 MultipartFile 文档的 List 为空。
请问有什么想法吗?
亲切的问候,
解决方案
此代码适用于来自 POSTMAN 的多个文件。
注意:请注意约定和空值检查。
@PostMapping("/upload")
public void uploadFileHandler(@RequestBody List<MultipartFile> documents, @RequestParam("easy") String easy)
throws Exception {
System.out.println(easy);
if(documents != null && !documents.isEmpty()) {
for(MultipartFile file : documents) {
storeFile(file);
}
}
}
public void storeFile(MultipartFile file) throws Exception {
if (!file.isEmpty()) {
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
System.out.println(fileName);
byte[] bytes = file.getBytes();
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File("C://Users//username//Desktop//upload//"+fileName)));
stream.write(bytes);
stream.flush();
stream.close();
}
}
推荐阅读
- github-actions - 要求对特定工作流程的所有外部合作者进行批准
- next.js - 我正在尝试运行我的 nextjs 应用程序,但收到错误 TypeError: Only absolute URLs are supported
- java - 如何在下面给出的代码中删除“[]”?预期输出是“pollTimestamp”
- python - 如何使用 Python API 从 Spark 中的一行创建列表
- c - C 程序错误代码混淆(比较值)
- apache-poi - Apache POI 是否不再维护 4.x 版本?
- python - 熊猫在两个数字之间填充 nan
- google-cloud-platform - 如果我将 Python 脚本用于 Google Product Search API,如何指定 max_results 参数?
- perl - Perl 递归解释
- python - 无法识别事件消息