spring MVC multiple files upload

jsp, spring-mvc

Solution

Just do like this. you dont need to have more than `input` tag in your form to select multiple files

  <input type="file" name="fileUpload" size="50" multiple/>    

It will allow users to select multiple files in their sytem by clicking `ctrl` option in keyboard.

And then, in your action class, do your stuff as you wanted.

Make sure `fileUpload` variable as file array in your bean class

Problem

I'm using spring MVC and JSP. I want to upload 2 files, issue is only one file is getting uploaded. Below is the code: ``` <form id="myform" name="myform" action="/createRequest.htm" enctype="multipart/form-data" method="POST"> //form elements like textbox, checkbox <tr> <th class="RelReqstAllign"></th><td> (Or)<input type="file" name="fileUpload" size="50"/></td> </tr> <tr> <th class="RelReqstAllign"></th><td><input type="file" name="fileUpload" size="50" /></td> </tr> </form> ``` Below is the spring controller code: ``` @RequestMapping(value = "/createRequest", method = RequestMethod.POST) public ModelAndView createRequest(final HttpServletRequest request, final HttpServletResponse response, final @ModelAttribute("spRequestDTO") SPRequestDTO dto, final BindingResult beException, final @RequestParam("buttonName") String buttonName, @RequestParam CommonsMultipartFile[] fileUpload) throws IOException { if (fileUpload != null && fileUpload.length > 0) { for (CommonsMultipartFile aFile : fileUpload) { System.out.println("Saving file: " + aFile.getOriginalFilename()); if (!aFile.getOriginalFilename().equals("")) { try { aFile.transferTo(new File(saveDirectory + aFile.getOriginalFilename())); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } } ``` When I debug the controller, `fileUpload` is showing only one file, even when I upload two files. Below is the code added in Spring-mvc.xml ``` <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> </bean> ```

Original source