Search this site:       RSS Feeds        Facebook
Submit to DeliciousSubmit to DiggSubmit to FacebookSubmit to Google BookmarksSubmit to StumbleuponSubmit to TechnoratiSubmit to TwitterSubmit to LinkedIn

Upload files with Spring MVC (Eclipse-based tutorial)

Most Recommended Books:      Effective Java      Thinking in Java      Head First Java      Head First Servlets and JSP

This tutorial is about how to implement file upload functionality with Spring MVC framework. To handle file uploads, Spring provides a MultipartResolver bean which is responsible for resolving multipart request. This resolver is working with two file upload libraries:

 

    • Apache Commons File Upload: The bean class is org.springframework.web.multipart.commons.CommonsMultipartResolver
    • COS (com.oreilly.servlet): The bean class is org.springframework.web.multipart.cos.CosMultipartResolver

 

We need to declare the MultipartResolver bean in Spring’s context file as follows:

 

For CommonsMultipartResolver:

 

<bean id="multipartResolver"
	class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	<!-- max file size in bytes -->
	<property name="maxUploadSize" value="2000000" />
	<!-- other properties... -->
</bean>

 

 And for CosMultipartResolver:

<bean id="multipartResolver"
	class="org.springframework.web.multipart.cos.CosMultipartResolver">
	<!-- max file size in bytes -->
	<property name="maxUploadSize" value="2000000" />
	<!-- other properties... -->
</bean>

Also we need to add to classpath jar files of the file upload library employed:

    • For Apache Commons File Upload: commons-fileupload-VERSION.jar and commons-io-VERSION.jar (Commons File Upload depends on Commons IO).
    • For COS: cos.jar

As Apache Commons File Upload is popular and preferred over COS, this tutorial will use it. We will build a Spring MVC application which has an upload form looks like in the following screenshot:

upload form

 

This tutorial requires the following pieces of software installed on your computer (click on a link to download the corresponding software):

 

 

Attachments:
Download this file (FileUploadSpringMVC.war)FileUploadSpringMVC.war[Deployable WAR file]3540 Kb
Download this file (FileUploadSpringMVC.zip)FileUploadSpringMVC.zip[Eclipse project]3542 Kb

Additional information