Please see this tutorial for an introduction to Spring MVC. In this tutorial we look at how to upload files using spring MVC framework. There are three things that need to be done to be enable file upload in a form.
- Add a file upload field in the form. This can be easily achieved by using a input element of type file.
. The form needs to be configured to submit files. This is achieved by changing the form type to multipart/form-data
- Handle uploaded file – To handle the uploaded file, the handler method needs to be declared with a parameter of type MultipartFile. This parameter accepts the uploaded file. Look at the method signature of the handler method in the example below
- Give the DispatcherServlet a multipart resolver (to take care of the MultipartFile parameter that we passed in 2 above). The DispatcherServlet declares a field called multipartResolver. Declare a bean of same name and it will be injected in the DispatcherServlet. Spring provides two implementations of MultipartResolvers – CommonsMultipartResolver and StandardServletMultipartResolver
The example below shows how to present a form that allows the user to upload an image.
Create the Person class which models a Person with his name and age. This represents the model in MVC pattern.
Create the PersonService class with a method to fetch a list of all the persons (see lines 19-21 below) and also to add a new Person (see lines 23-25 below). PersonService class will be used by PersonController (described later) to add a new Person and also fetch the list of all persons.
Create the PersonController class. This class is the Controller in MVC pattern and is declared as such by using the
annotation (see line 22 below).
When a request URL ends with
then Spring MVC passes the control to
method (see lines 51-74 below) by using the
annotation (see line 50 below).
In particular, note that the request parameter called ‘image’ is handled using
(see line 53 below). This is used in uploading the image file.
Validate that the image is of type JPEG(see line 56-62 and lines 76-80 below).
Finally, save the image to the file system (see line 65). This file is stored in the servlet context path of the web application (see lines 88-100 below).
This demonstrates the handling of file uploads.
Create the new_person.jsp as shown below which allows the user to add a new Person. This represents the view in MVC pattern.
Set the encoding type as
to allow for file uploading (see line 9 below)
Create and HTML form to take name, age and image of the person as inputs (see lines 10-31 below).
When the ‘Submit’ button is pressed, then call the ‘/springmvcfileupload/person/add’ URL to create a new Person (see line 8 below).
Create the person_list.jsp as shown below which displays the list of all Persons. This represents the view in MVC pattern.
Get the list of persons by using the name of the model
(see line 17 below). Note that this model was set in PersonController.showPersonListPage() method with the same model name.
Iterate the list persons and display the list in a table (see lines 18-23 below).
Create the web.xml configuration as shown below.
Create a servlet mapping with name
(see lines 13-16 below) and define the corresponding servlet using Spring
class (see lines 7-11 below).
Note that Spring automatically will look for a configuration file called
corresponding to the servlet mapping name person. We shall describe the details of
Create the Spring configuration by the name
as shown below.
Configure Spring such that annotation based wiring will be used (see lines 14-15 below).
Configure Spring default validator which will perform validations (see line 17 below) defined in Person class
Configure Spring such that the prefix
and the suffix
should be added to the name of the view JSPs specified in various methods in PersonController (see lines 19-27 below).
To handle file uploads, configure the MultipartResolver along with the maximum permissible file size (see lines 30-32 below).
This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies)on your machine and automatically run the program for you as shown in the steps below. As this sample program contains Java Server Pages (JSPs), you will need Java Development Kit (JDK preferably 1.5 or higher) on your machine so that the JSPs can be complied locally. Note that no other setup is required on your machine! Also please ensure that the port 8080 is not being used by any other program on your machine.
(Alternatively you can go the folder containing the springmvcfileupload-installer.jar and execute the jar using
java -jar springmvcfileupload-installer.jar
This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
. All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.