我正在尝试在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文档的列表为空。
有什么想法吗?
亲切的问候,
答案 0 :(得分:1)
此代码对我来说适用于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();
}
}