Spring MVC Tutorial: How to Upload Multiple Files to Specific Location

This is another complete Spring MVC tutorial which accepts file on Upload form and copy it to specific folder on “Submit” event. As usual we have a dependency on Hello World Spring MVC Example.

Crunchify Spring MVC Multiple file upload Example Spring MVC Tutorial: How to Upload Multiple Files to Specific Location

So, these are the additions / changes we need to perform in this example:

  • New file: CrunchifyFileUploadController.java
  • New file: CrunchifyFileUpload.java
  • New file: uploadfile.jsp
  • New file: uploadfilesuccess.jsp
  • Modified file: crunchify-servlet.xml
  • 2 new jar files: commons-io-2.4.jar and commons-fileupload-1.3.jar

Now let’s get started:

Step1: Pre-Requisite: http://crunchify.com/hello-world-example-spring-mvc-3-2-1/ (Deploy this project successfully on Tomcat)

Step2: SpringController 

Create a Spring 3 MVC based controller which handles file upload. There are two methods in this controller:

  1. crunchifyDisplayForm – It simply forwards request to the pageuploadfile.jsp
  2. crunchifySave – Fetches the form using @ModelAttribute annotation and get the File content from it. It creates a list of filenames of files being uploaded and pass this list to success page.

Step3: Model – Form Object

Create a Java bean which acts as Model/Form object for our Spring application. This bean contains a List of org.springframework.web.multipart.MultipartFile objects. Spring framework provides a useful class MultipartFile which can be used to fetch the file content of uploaded file. Apart from its content, the MultipartFile object also gives you other useful information such as filename, file size etc.

Step4: JSP Views

Now create the view pages for this application. We will need two JSPs, one to display file upload form and another to show result on successful upload.

The uploadfile.jsp displays a form with file input. Apart from this we have added small jquery snippet onclick of Add button. This will add a new file input component at the end of form. This allows user to upload as many files as they want.

Note that we have set enctype=”multipart/form-data” attribute of our <form> tag.

Step5: Update Spring Configuration

Add below bean to crunchify-servlet.xml  file, just above   <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">  line.

Step6: Checkout Result.

Start tomcat and point your browser to this URL: http://localhost:8080/CrunchifySpringMVC3.2.1/upload.html and you should see screen similar to screenshot at the top of this page.

After file upload you will see success message like this. You can always beautify your jsp file the way you want.

Crunchify Spring MVC Multiple file upload Example Result Spring MVC Tutorial: How to Upload Multiple Files to Specific Location

List of all Spring MVC Examples, Java Examples.

If you enjoyed this post, make sure to subscribe to Crunchify's RSS feed.

Enjoyed this post?

Be sure to subscribe to the Crunchify newsletter and get regular updates about awesome posts just like this one and more!