Reputation: 3270
I'm trying to pass a list of files in a Post request as follow:
PostMan:
Controller:
@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 {
....
....
}
All passed parameters and variables are not null, only the List of the MultipartFile documents is empty.
Any idea please?
Kind regards,
Upvotes: 0
Views: 620
Reputation: 7624
This code worked for me for multiple files from POSTMAN.
Note: Please take care of conventions and null checks.
@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();
}
}
Upvotes: 1