Springs

Spring MVC File Upload Example Tutorial

File Uploading is a very common task in any web application. Today we will learn about Spring File upload, specifically Spring MVC File Upload for single and multiple files.

Spring uses MultipartResolver interface to handle the file uploads in web application. Below are two of the implementation :

  1. StandardServletMultipartResolver – Servlet 3.0 multipart request parsing.
  2. CommonsMultipartResolver – Classic commons-fileupload.jar

 

Tools used in this article :

    1. Spring 4.3.5.RELEASE
    2. Maven 3
    3. Tomcat 7

1. Project Structure

Spring mvc file upload project structure

2. Project Dependency

First of all we need to add project dependency in our pom.xml

 

3. Spring MVC configuration

To enable file upload we will add MultiPart bean in our spring mvc configuration file springDispatcherContext.xml  org.springframework.web.multipart.commons.CommonsMultipartResolver

Our final Spring configuration file looks like below.

springDispatcherContext.xml

 

Notice I am setting max upload file size for CommonsMultiPartResolver using “maxUploadSize”

4. Spring File Upload Controller Class

Controller class is the main class of our Spring MVC File Upload example which controls teh file upload process. Controller class should have handler method to return view and also to process file upload.

 

5. Spring View – upload.jsp

 

 

6. Web.xml

 

 

7. Spring File upload Result

Type below url in browser and open welcome page to upload file.

http://localhost:8080/springFileUpload/welcome

Upload success:

 

Also Read about Spring MVC Excel file upload to database here.

 

Download Sample from GitHub.com

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.