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 java.io.Serializable {
private String name;
private List files;
// Must generate getter/setters here
}

3. Define the JSP View for Form
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form" %>

Name: 
Picture 1 :  
Picture 2 :  


4. Just get your files in Controller
@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.

    ReplyDelete