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.

Spring MVC Upload Multiple Files

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

Here is a final project structure so you will get some idea on where to add files.

Spring MVC File Upload Tutorial by Crunchify

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)

Maven Dependencies:

Add below new dependencies to your project’s pom.xml file.

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 this.

Crunchify Spring MVC - Multiple file upload Example

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

List of all Spring MVC Examples, Java Examples.

Have anything to add to this article? Please chime in and join the conversion.

Enjoyed this post?

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

  • Hamza Ouni

    Hi , Could you help me i want to store the path of the files to database ?

  • Hamza Ouni

    Hi ,By the way it was a good tuto , but i need to upload multiple files to database , Could you help me please , I am working on my final year project work .

  • Kiheung Park

    One quick question, what is modelAttribute=”uploadForm” doing? Is it just the name of the model attribute or does it has to be exactly match with other name?

    • http://crunchify.com/ App Shah

      @ModelAttribute as the equivalent of @Autowired + @Qualifier i.e. it tries to retrieve a bean with the given name from the Spring managed model. If the named bean is not found, instead of throwing an error or returning null, it implicitly takes on the role of @Bean i.e. Create a new instance using the default constructor and add the bean to the model.

      Name should match with ModelAttribute value in file uploadfile.jsp

  • Kiheung Park

    where do i put fileuploadcontroller.java and fileupload.java?
    1. in package called com.crunchify.controller?
    2. in package called com.crunchify.form?

    what about the two jsp files?

    1. under WEB-INF/jsp/?
    2. WebContent?

    • http://crunchify.com/ App Shah

      I’ve just updated above tutorial with complete Eclipse project structure for reference. Let me know for any query.

  • Kiheung Park

    Hi,

    I’ve done CrunchifyHelloWorld tutorial and then trying this one, upload multiple files example.

    I am having trouble with “org.springframework.web.servlet.PageNotFound noHandlerFound” that warns me there are no mapping found for HTTP request with URI [/CrunchifySpringMVCTutorial/uploadfile.html] in DispatcherServlet with name ‘crunchify’

    I did everything says in the above instruction.

    [Question#1]
    Where do I place two jsp files(uploadfile.jsp and uploadfilesuccess.jsp?
    Should I put them under WEB-INF/jsp/ or /WebContent?

    [Question#2]
    Where should I create ‘CrunchifyFileUploadController.java’?
    I’ve created that class on ‘com.crunchify.controller’ just in case.
    And created another package (com.crunchify.form) for ‘CrunchifyFileUpload.java’.

    Thank you.

    Ki

    • http://crunchify.com/ App Shah

      Hi Kiheung Park. I’ve just updated above tutorial and add all required information + project screenshot. Please let me know if you have any query.

      • Kiheung Park

        Thank you!!

  • Ashok k

    Hi
    how play the audio files randumly in spring mvc validation

  • Radhika Agarwal

    Hi,
    I have a similar query as below.I want to insert image url in mysql DB using hibernate and spring mvc but i am using multipart file upload. Please guide me in doing this.

    • http://crunchify.com/ App Shah

      Hi Radhika – just wondering do you want to insert image URL or actual image? Multipart form data should work in image case also.

  • Naveen Kumar Mishra

    Please guide me how to upload images in database

    • http://crunchify.com/ App Shah

      Hi Naveen – Sorry for late reply.

      In CrunchifyFileUploadController.java in if condition you need to add your DB connectivity, Copy image/file to BLOB type in to DB. As I don’t have any information which DB you are using or environment, would be difficult to provide you code here. Please email me with some more information and would definitely reply to your with solution in a day or so. Thanks.

      • Naveen Kumar Mishra

        I want to store image/txt file in mysql database by using SpringMVC 3.0 and Hibernate 3.1.

      • Naveen Kumar Mishra

        i want to store images in mysql database by using SpringMVC 3.0 and hibernate 3.1 so please help me …….

        • http://crunchify.com/ App Shah

          Please export project and send me via email. Your requirement is very generic to answer it here :)

  • Naveen Kumar Mishra

    This is good but may you guide me how to store images in database……