Reputation: 1159
I have a Spring MVC REST controller that accepts a multipart file, as follows:
@Consumes(MediaType.MULTIPART_FORM_DATA)
@RequestMapping(value = "/save-comment", method = RequestMethod.POST)
public String addComment(@FormDataParam("jsonData") String jsonData, @FormDataParam("file") MultipartFile file, ModelMap model)
{
//My Logic to save file and data
}
I use Jersey REST client in my application. The above code works fine for me. Now I am trying to POST multiple files to my REST controller.
I tried to change @FormDataParam("file") MultipartFile file
to @FormDataParam("file") MultipartFile[] file
but it is not working for me. How can I pass multiple files at a time to a REST controller?
The exception I get is: nested exception is
org.springframework.beans.BeanInstantiationException: Failed to instantiate [[Lorg.springframework.web.multipart.MultipartFile;]: No default constructor found; nested exception is java.lang.NoSuchMethodException: [Lorg.springframework.web.multipart.MultipartFile;.<init>()] with root cause
java.lang.NoSuchMethodException: [Lorg.springframework.web.multipart.MultipartFile;.<init>()
Upvotes: 1
Views: 6687
Reputation: 93
Multiple File Upload concept , we can handle by using MultipartFile Interface.
Package: org.springframework.web.mutipart public interface MutipartFile
MutipartFile is an interface, Commonly Client will send or upload a file , it will be sent to server as in the form of mutipart request.
We will catch that mutipart request by using MutipartFile Concept. This MutipartFile Interface have number of method , those methods are used to get information of that file and if you want to do any copy or moving of a file also we can perform by one of the method called transferTo(“destinationPath”) ;
for better understanding purpose just visit https://walkintoknow.blogspot.com/2018/05/multiple-files-upload-concept-handling.html
@RequestMapping(value="/multipleFilesUpload" , method=RequestMethod.POST,
consumes="multipart/form-data", produces="application/json")
public ResponseEntity<?> mutipleFileUpload(HttpServletRequest req,
@RequestParam(value="file" , required = false) MultipartFile[] files) throws IOException{
for (MultipartFile file : files) {
File f= new File(getPath()+createFolderInDesc("/appFiles /GSTC/mutipleUpload/"),file.getOriginalFilename());
try {
file.transferTo(f); //Transfer or Saving in local memory
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
if it helps please promote.
Upvotes: 2
Reputation: 15908
Just typecast normal http request to multipart request like below :
try {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) httpServletRequest;
List<MultipartFile> multipartFileList = multipartRequest
.getFiles("images");
if (null != multipartFileList && !multipartFileList.isEmpty()) {
for (MultipartFile file : multipartFileList) {
String fileName = file.getOriginalFilename().trim();
if (file.getBytes().length > 0) {
// logic goes gere
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
Upvotes: 1