File Uploading is a very common task in any web application. We have earlier seen how to upload files in Servlet and Struts2 File Uploading. Today we will learn about Spring File upload, specifically Spring MVC File Upload for single and multiple files.

Spring MVC File Upload

Spring MVC framework provides support for uploading files by integrating Apache Commons FileUpload API. The process to upload files is very easy and requires simple configurations. We will create a simple Spring MVC project in STS that will look like below image.

Most of the part is the boiler-plate code generated by STS tool, we will focus on the changes that are required to utilize Spring file upload integration.

Maven Dependencies for Apache Commons FileUpload

First of all, we need to add Apache Commons FileUpload dependencies in our pom.xml file, so that required jar files are part of the web application. Below is the dependency snippet from my pom.xml file.

Spring File Upload Form Views

We will create two JSP pages to allow single and multiple file uploads in spring web application.

upload.jsp view code:

uploadMultiple.jsp view code:

Notice that these files are simple HTML files, I am not using any JSP or Spring tags to avoid complexity. The important point to note is that form enctype should be multipart/form-data, so that Spring web application knows that the request contains file data that needs to be processed.

Also note that for multiple files, the form field “file” and “name” are the same in the input fields, so that the data will be sent in the form of an array. We will take the input array and parse the file data and store it in the given file name.

Spring MVC Multipart Configuration

To utilize Apache Commons FileUpload for handling multipart requests, all we need to do is configure multipartResolver bean with class as org.springframework.web.multipart.commons.CommonsMultipartResolver.

Our final Spring configuration file looks like below.

servlet-context.xml code:

Notice that I am setting maximum upload size limit by providing the maxUploadSize property value for multipartResolver bean. If you will look into the source code of DispatcherServlet class, you will see that a MultipartResolver variable with name multipartResolver is defined and initialized in below method.

With this configuration, any request with enctype as multipart/form-data will be handled by multipartResolver before passing on to the Controller class.

Spring File Upload Controller Class

Controller class code is very simple, we need to define handler methods for the uploadFile and uploadMultipleFile URIs.

FileUploadController.java code:

Notice the use of Spring annotations that make our life easier and code looks more readable.

uploadFileHandler method is used to handle single file upload scenario whereas uploadMultipleFileHandler method is used to handle multiple files upload scenario. Actually we could have a single method to handle both the scenarios.

Now export the application as WAR file and deploy it into Tomcat servlet container.

When we run our application, below images shows us the request and responses.

Spring MVC File Upload Example

Spring-File-Uplaod-Project

Spring-File-Uplaod-Project

You can check the server logs to know the location where the files have been stored.

Download the project from the above link and play around with it to learn more.

By admin

Leave a Reply

%d bloggers like this: