Spring MVC does special care to upload file to server. It makes file upload an easy work for web application developers. Spring MVC library jar provides CommonsMultipartResolver class that makes special care to form submitted using “multipart/form-data” encode type. We can also specify max file size in this resolver.
Tools Used:
- Spring MVC 3.0.3
- Eclipse Indigo 3.7
- Tomcat 6
- Jdk 1.6
- Apache Common File Upload 1.2.0
- Apache Common IO 2.0.1
As you can see, we need two more jar files to upload file to server using Spring MVC. The name of the jar files are :
- commons-io-2.0.1.jar
- commons-fileupload-1.2.1.jar
Step1: Registering CommonsMultipartResolver in Spring MVC Configuration file
We must first register CommonsMultipartResolver class in Spring MVC configuration file so that Spring can handle multipart form data.
1 |
<!-- Configure the multipart resolver --> |
2 |
< bean id = "multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" > |
4 |
<!-- one of the properties available; the maximum file size in bytes --> |