-1

I am using Spring-rest at server side. here i am handling request from android app. but , i am unable to receive multipart files uploaded from android app. android app developers sending multiple files as in the form of List. for receiving that request at server side i used below code.

 @RequestMapping(value="/multipleFilesUpload" , method=RequestMethod.POST, 
                consumes="multipart/form-data", produces="application/json")  
public ResponseEntity<?> mutipleFileUpload(HttpServletRequest req, 
                                           @RequestParam(value="files" , required = false) List<MultipartFile> files,
                                           @RequestParam("desc") String desc) throws IOException{
    System.out.println("Hits::"+desc);
    for (int i = 0; i < files.size(); i++) {
        System.out.println(files.get(i).getOriginalFilename());
    }



    return null;
} 

But, i am getting empty List. Please , give a suggestion how to receive multiple files in spring - rest

1 Answers1

1

Since its a multipart/form-data then the method parameters should be @RequestPart not @RequestParam, and you method might look like below where list of MultiPart File will come in @RequestPart(value="files" , required = false) MultipartFile... files and @RequestPart("desc") String desc will be the JSON part of the payload.

@RequestMapping(value="/multipleFilesUpload" , method=RequestMethod.POST, 
                consumes="multipart/form-data", produces="application/json")  
public ResponseEntity<?> mutipleFileUpload(HttpServletRequest req, 
                                           @RequestPart(value="files" , required = false) MultipartFile... files,
                                           @RequestPart("desc") String desc) throws IOException{
    System.out.println("Hits::"+desc);

    List<MultipartFile> fileList = Arrays.asList(files);
    for (int i = 0; i < fileList.size(); i++) {
        System.out.println(fileList.get(i).getOriginalFilename());
    }



    return null;
}
Amit K Bist
  • 6,760
  • 1
  • 11
  • 26