Monday, May 21, 2012

How to Upload Multiple Files using Spring MVC 3.1

Here I share only related parts of the code:
1. Declare Multipart Resolver in spring config file.

2. Define FormBean:
public class User implements {
private String name;
private List files;
// Must generate getter/setters here

3. Define the JSP View for Form
<%@ taglib prefix="f" uri="" %>
<f:form action="/users/new" enctype="multipart/form-data" method="POST" modelattribute="user" name="user">
Name:<f:input path="name"> 
Picture 1 : <f:input path="files" type="file"> 
Picture 2 : <f:input path="files" type="file"> 
<input name="Submit" type="submit" />

4. Just get your files in Controller
@RequestMapping ("/users")
public class UsersController {
 @RequestMapping (value = "/new", method = RequestMethod.POST)
    public String newUser(@ModelAttribute User user) {
        for (MultipartFile file: user.getFiles()) {
            System.out.println("file name: " + file.getOriginalFilename());
            // access other file attributes here ... 

1 comment:

  1. This is not working for me.Not able to get the list of files.
    I am able to get only first file in my action.