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" %>
<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" />
</f:input></f:input></f:input></f:form>


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 ...
}
}
}

Comments

  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

Post a Comment