Spring 4 MVC - File upload example with Commons FileUpload



Spring 4 MVC - File upload example with Commons FileUpload

Technologies used:   Java SE 1.8 | Spring 4.3.7.RELEASE | Commons Fileupload 1.3.2 | Maven 3.3.9 | Apache Tomcat 7.0.47 | Eclipse Neon.3 

In our previous post Spring 4 MVC - File upload example with Servlet 3.0, we have learned how to upload a single and multiple files in Spring MVC application using the StandardServletMultipartResolver, which is standard implementation of the org.springframework.web.multipart.MultipartResolver.

In this section, we will show you how to use the CommonsMultipartResolver for handling multipart request in Spring MVC application.

The CommonsMultipartResolver is Servlet base MultipartResolver implementation for Apache Common FileUpload. You need to add commons-fileupload.jar in your classpath to use the CommonsMultipartResolver in Spring application.

 

Jar dependencies

Open pom.xml file of your maven project and add the following dependencies in it .

<dependencies>
  <!-- Spring MVC Dependency -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.7.RELEASE</version>
  </dependency>

  <!-- Commons Fileupload -->
  <dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.2</version>
  </dependency>
  <!-- JSTL Dependency -->
  <dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>javax.servlet.jsp.jstl-api</artifactId>
    <version>1.2.1</version>
  </dependency>
  <dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
  </dependency>

  <!-- Servlet Dependency -->
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
  </dependency>

  <!-- JSP Dependency -->
  <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
    <scope>provided</scope>
  </dependency>
</dependencies>

 

Controller class

Create a controller class to handle the single and multiple files upload request as follows.

FileUploadController.java

package com.boraji.tutorial.spring.controller;

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

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

/**
 * @author imssbora
 */
@Controller
public class FileUploadController {

   @GetMapping("/")
   public String fileUploadForm(Model model) {

      return "fileUploadForm";
   }

   // Handling single file upload request
   @PostMapping("/singleFileUpload")
   public String singleFileUpload(@RequestParam("file") MultipartFile file, Model model)
         throws IOException {

      // Save file on system
      if (!file.getOriginalFilename().isEmpty()) {
         BufferedOutputStream outputStream = new BufferedOutputStream(
               new FileOutputStream(
                     new File("D:/SingleFileUpload", file.getOriginalFilename())));
         outputStream.write(file.getBytes());
         outputStream.flush();
         outputStream.close();

         model.addAttribute("msg", "File uploaded successfully.");
      } else {
         model.addAttribute("msg", "Please select a valid file..");
      }

      return "fileUploadForm";
   }

   // Handling multiple files upload request
   @PostMapping("/multipleFileUpload")
   public String multipleFileUpload(@RequestParam("file") MultipartFile[] files,
         Model model) throws IOException {

      // Save file on system
      for (MultipartFile file : files) {
         if (!file.getOriginalFilename().isEmpty()) {
            BufferedOutputStream outputStream = new BufferedOutputStream(
                  new FileOutputStream(
                        new File("D:/MultipleFileUpload", file.getOriginalFilename())));

            outputStream.write(file.getBytes());
            outputStream.flush();
            outputStream.close();
         } else {
            model.addAttribute("msg", "Please select at least one file..");
            return "fileUploadForm";
         }
      }
      model.addAttribute("msg", "Multiple files uploaded successfully.");
      return "fileUploadForm";
   }
}

 

JSP View

Create a fileUploadForm.jsp file under src\main\webapp\WEB-INF\views folder to select single or multiple files for sending to controller’s handler method.

fileUploadForm.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>BORAJI.COM</title>
</head>
<body>

  <h1>Spring MVC - File Upload Example</h1>
  <hr />

  <h3>Single file Upload</h3>
  <form action="singleFileUpload" method="post" enctype="multipart/form-data">
    <table>
      <tr>
        <td>Select File</td>
        <td><input type="file" name="file"></td>
        <td><button type="submit">Upload</button></td>
      </tr>
    </table>
  </form>
  <br />
  <hr />
  <h3>Multiple file Upload</h3>
  <form action="multipleFileUpload" method="post" enctype="multipart/form-data">
    <table>
      <tr>
        <td>Select Files</td>
        <td><input type="file" name="file" multiple="multiple"></td>
        <td><button type="submit">Upload</button></td>
      </tr>
    </table>
  </form>
  <br>
  <hr />
  <span style="color: red; font-size: 14px;">${msg}</span>

</body>
</html>

 

Spring configuration

Create a web @Configuration class annotated with @EnableWebMvc and @ComponentScan as follows. 

To support multipart handling in Spring application, declare CommonsMultipartResolver bean method in configuration class.

WebConfig.java

package com.boraji.tutorial.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
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;

/**
 * @author imssbora
 */

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.boraji.tutorial.spring.controller" })
public class WebConfig extends WebMvcConfigurerAdapter {

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

   @Bean
   public MultipartResolver multipartResolver() {
      CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
      multipartResolver.setMaxUploadSize(10485760); // 10MB
      multipartResolver.setMaxUploadSizePerFile(1048576); // 1MB
      return multipartResolver;
   }

}

 

Servlet container initialization

Create a container initializer class by extending the AbstractAnnotationConfigDispatcherServletInitializer class as follows.

MyWebAppInitializer.java

package com.boraji.tutorial.spring.config;

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

/**
 * @author imssbora
 */
public class MyWebAppInitializer
      extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

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

The AbstractAnnotationConfigDispatcherServletInitializer class, implements the WebApplicationInitializer, is implemented in Servlet 3.0+ environments in order to configure the ServletContext programmatically.

 

Build + Deploy + Run application

Use the following maven commands to build, deploy and run embedded Tomcat server.

mvn clean install  (This command triggers war packaging)

mvn tomcat7:run (This command run embedded tomcat and deploy war file automatically)

You can refer this link to learn how to run the above commands in Eclipse IDE.


Type the following URLs in browser's address bar to open the file upload from.

http://localhost:8080/

Spring 4 MVC - File upload example | BORAJI.COM

Select single file and click on Upload button.

Spring 4 MVC - File upload example | BORAJI.COM

Select multiple files and click on Upload button.

Spring 4 MVC - File upload example | BORAJI.COM