• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

Spring MVC Multiple File Upload Example | CommonsMultipartResolver

October 22, 2018 by javainterviewpoint Leave a Comment

In this Spring MVC Multiple File Upload Example, we will learn how to upload multiple files in Spring MVC using CommonsMultipartResolver. We must add the Apache Commons File Upload dependency (commons-fileupload.jar) in order to use CommonsMultipartResolver.

Spring by default will not handle multipart file uploads, however it provides the support to multipart using the pluggable multipart object called “MultipartResolver”. We will have to be enabling the MultipartResolver in the context, in our case it is CommonsMultipartResolver.

Once we have enabled the MultipartResolver in the context each request will be checked whether it has a multipart in it, if present then the configured CommonsMultipartResolver will be used.

Folder Structure:

Spring MVC Multiple File Upload Example

  1. Create a simple Maven webapp Project “SpringMVCFileUpload” and create a package for our source files “com.javainterviewpoint” under  src/main/java 
  2. Now add the following dependency in the POM.xml
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>com.javainterviewpoint</groupId>
    	<artifactId>SpringMVCFileUpload</artifactId>
    	<packaging>war</packaging>
    	<version>0.0.1-SNAPSHOT</version>
    	<name>Spring MVC Multiple File Upload Example</name>
    	<url>http://maven.apache.org</url>
    
    	<properties>
    		<jdk.version>1.8</jdk.version>
    		<spring.version>4.3.7.RELEASE</spring.version>
    		<jstl.version>1.2</jstl.version>
    		<servlet.version>3.1.0</servlet.version>
    		<commons.fileupload.version>1.3.2</commons.fileupload.version>
    	</properties>
    
    	<dependencies>
    		<!-- Spring MVC Dependency -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-webmvc</artifactId>
    			<version>${spring.version}</version>
    		</dependency>
    
    		<!-- Apache Commons file upload Dependency-->
    		<dependency>
    			<groupId>commons-fileupload</groupId>
    			<artifactId>commons-fileupload</artifactId>
    			<version>${commons.fileupload.version}</version>
    		</dependency>
    
    		<!-- JSTL Dependency -->
    		<dependency>
    			<groupId>jstl</groupId>
    			<artifactId>jstl</artifactId>
    			<version>${jstl.version}</version>
    		</dependency>
    
    		<!-- Servlet Dependency -->
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>javax.servlet-api</artifactId>
    			<version>${servlet.version}</version>
    			<scope>provided</scope>
    		</dependency>
    
    	</dependencies>
    	<build>
    		<finalName>SpringMVCFileUpload</finalName>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<version>3.3</version>
    				<configuration>
    					<source>${jdk.version}</source>
    					<target>${jdk.version}</target>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>
    </project>
  3. Create the Java class UploadController.java,WebApplicationInitializer.java and SpringWebMvcConfig.java under  com.javainterviewpoint folder.
  4. Place the uploadForm.jsp under the sub directory under WEB-INF/Jsp

Other interesting articles which you may like …

  • Spring MVC 5 Thymeleaf 3 Hello World Example
  • Spring MVC CRUD Example with MySql + JdbcTemplate
  • AngularJS Spring MVC CRUD – $http service
  • AngularJS Spring MVC integration
  • Spring MVC Form Validation -Annotations and ResourceBundle
  • Spring MVC Exception Handling @ExceptionHandler
  • Spring MVC Exception Handling @ControllerAdvice and @ExceptionHandler
  • Spring MVC Custom Exception Handling
  • context:annotation-config vs context:component-scan
  • Spring MVC BeanNameUrlHandlerMapping Example
  • Spring MVC ControllerClassNameHandlerMapping Example
  • Spring MVC SimpleUrlHandlerMapping Example
  • Spring MVC Flow Diagram
  • Spring MVC Multiple submit buttons in a single form
  • Spring MVC SimpleFormController Example
  • Spring 4 – Spring MVC Hello World Example
  • Spring REST Hello World Example – JSON and XML responses
  • ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
  • Fix missing src/main/java folder in Eclipse Maven Project – 2 build path entries are missing

Spring MVC Multiple File Upload Example

Dependency Tree

[INFO] ------------------------------------------------------------------------
[INFO] Building Spring MVC Multiple File Upload Example 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ SpringMVCFileUpload ---
[INFO] com.javainterviewpoint:SpringMVCFileUpload:war:0.0.1-SNAPSHOT
[INFO] +- org.springframework:spring-webmvc:jar:4.3.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.3.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.3.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:4.3.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:4.3.7.RELEASE:compile
[INFO] |  |  \- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  +- org.springframework:spring-expression:jar:4.3.7.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:4.3.7.RELEASE:compile
[INFO] +- commons-fileupload:commons-fileupload:jar:1.3.2:compile
[INFO] |  \- commons-io:commons-io:jar:2.2:compile
[INFO] +- jstl:jstl:jar:1.2:compile
[INFO] \- javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] ------------------------------------------------------------------------

WebMvcConfiguration.java

Create our SpringWebMvcConfig.java under the com.javainterviewpoint package.

package com.javainterviewpoint;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@EnableWebMvc
@Configuration
@ComponentScan({"com.javainterviewpoint"})
public class SpringWebMvcConfig extends WebMvcConfigurerAdapter {

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/Jsp");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Bean
    public CommonsMultipartResolver multipartResolver() 
   {
        CommonsMultipartResolver resolver= new CommonsMultipartResolver();
        resolver.setMaxUploadSize(1048576); //1MB
        return cmr;
    }
}
  • We have created the new instance for the CommonsMultipartResolver and have set the setMaxUploadSize to permit only 1 MB.
  • Whenever the DispatcherServlet detects a multipart request, it activates the CommonsMultipartResolver which is declared in the Spring Context.
  • The MultipartResolver then wraps the plain HttpServletRequest into a MultipartHttpServletRequest which has support for multipart file uploads.

We have annotated our WebMvcConfiguration class with the below annotation

  1. @Configuration indicates that our SpringWebMvcConfig class can be used by the Spring IoC container as a source of bean definitions.
  2. @EnableWebMvc is equivalent to <mvc:annotation-driven /> in XML. It enables support for @Controller annotated classes. This annotation imports configuration from WebMvcConfigurationSupport
  3. @ComponentScan scans the stereotype annotations specified in @Controller, @Service etc.. annotated classes.

Equivalent XML configuration – SpringConfig-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<context:component-scan base-package="com.javainterviewpoint"> </context:component-scan>
	<mvc:annotation-driven> </mvc:annotation-driven>

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/Jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<bean id="multipartResolver" 
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

		<!-- Setting the Maximum upload size to 1MB -->
		<property name="maxUploadSize" value="1048576" />

	</bean>
</beans>

WebApplicationInitializer.java

We can use AbstractAnnotationConfigDispatcherServletInitializer class to register and initialize the DispatcherServlet when the Servlet used in greater than 3.0 (No need for web.xml)

package com.javainterviewpoint;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebApplicationInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringWebMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

}

Equivalent XML configuration – web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

    <servlet>
        <servlet-name>SpringConfig</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>SpringConfig</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/SpringConfig-servlet.xml</param-value>
    </context-param>
	
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

UploadController.java

package com.javainterviewpoint;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadController
{
    @GetMapping("/")
    public String showUploadForm(Model model)
    {
        return "uploadForm";
    }

    @PostMapping("/save")
    public String saveFiles(@ModelAttribute("uploadFiles") FileData fileData, Model model) throws IOException
    {
        //Get the list of files
        List files = fileData.getFiles();

        //Check whether the list is not null or empty
        if (files != null && !files.get(0).getOriginalFilename().isEmpty())
        {
            //Get the individual MultipartFile
            for (MultipartFile multipartFile : files)
            {
                //Write each MultipartFile in the directory specified
                if (!multipartFile.getOriginalFilename().isEmpty())
                {
                    BufferedOutputStream outputStream = new BufferedOutputStream(
                            new FileOutputStream(new File("E:\\JIP\\", multipartFile.getOriginalFilename())));

                    outputStream.write(multipartFile.getBytes());
                    outputStream.flush();
                    outputStream.close();
                }
            }
            model.addAttribute("message", "All Files are uploaded successfully!!");
        } 
        else
        {
            model.addAttribute("message", "Please select atleast one file!!");
        }

        return "uploadForm";
    }
}
  • We have annotated our “UploadController” class with @Controller annotation which tells Spring Container to treat this class as a Controller.
  • @GetMapping annotation on top of showUploadForm() redirects the request to this method, when the request given is “/” and it can take only GET request and redirects the user to the uploadForm.jsp
  • Once the user submits the form, the saveFiles() method will be called, it gets the form data using @ModelAttribute and in-turn iterates each MultipartFile and writes to the new directory.

uploadForm.jsp

The uploadForm.jsp should be created under /WEB-INF/Jsp folder.

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>

<body>
	<h1>Spring MVC Multiple File Upload Example</h1>

	<form:form method="POST" action="save" modelAttribute="uploadFiles"
		enctype="multipart/form-data">

		<input type="file" name="files" />
		<br><br>
		<input type="file" name="files" />
		<br><br>
		<input type="submit" value="Submit" />

	</form:form>
	<br>
	<hr />
	<h3 style="color: red;">${message}</h3>

</body>
</html>

In form, the modelAttribute holds the form data using FileData bean and we have also we have added the encoding attribute (enctype=”multipart/form-data”) which is necessary to let the browser know how to encode the multipart fields

Output

Once the server is started, hit on the url : http://localhost:8080/SpringMVCFileUpload/

Spring MVC Multiple File Upload Example 1

When the user hits on submit button without selecting a single file, the below error message will be thrown

Spring MVC Multiple File Upload Example 2

On successful upload

Spring MVC Multiple File Upload Example 3

    Download Source Code

Filed Under: Java, Spring MVC, Spring Tutorial Tagged With: Apache Commons FileUpload, CommonsMultipartResolver, multipart file, MultipartResolver, Spring MVC Multiple File Upload, Spring MVC Multiple File Upload Example

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·