Open In App

Spring MVC File Upload

Improve
Improve
Like Article
Like
Save
Share
Report

Spring MVC architecture uses the “FrontController” design pattern which is fundamental to any MVC design implementation. The DispatcherServlet is at the heart of this design whereby HTTP requests are delegated to the controller, views are resolved to the underlying view technology, in addition to providing support for uploading files. The DispatcherServlet like any regular servlet can be configured along with custom handler mappings.

Note: Spring MVC acts as a pre-requisite

Spring MVC framework provides the support for CommonsMultipartResolver for uploading any kind of file for a web-based application. Here we will be creating a Spring MVC web application and configure MultipartResolver to upload file (image) and also show them on the web.

Implementation:

Tip: We have used Eclipse ide for this project.

Steps to Create a Spring MVC File Uploading Project

Step 1: We need to create a maven webapp project. Now, click on create Maven project and add maven webapp archetype as the base package. Enter the group id and the artifact id for your project and click finish. 

The project structure would look something like this:

Step 2: Let’s start by adding some dependencies into the pom.xml already created after creating a maven project. The pom.xml defines all the dependencies that maven has to get and manage for you. The commons-fileupload and the commons-io are the dependencies important for MultipartResolver. Add all the dependencies according to this:

Dependency File: pom.xml

XML




<?xml version="1.0" encoding="UTF-8"?>
  
  <modelVersion>4.0.0</modelVersion>
  
  <groupId>com.gfg</groupId>
  <artifactId>SpringMVCFileUpload</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <name>SpringMVCFileUpload Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-webmvc</artifactId>  
    <version>5.1.1.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>org.apache.tomcat</groupId>  
    <artifactId>tomcat-jasper</artifactId>  
    <version>9.0.12</version>  
</dependency>  
      
<dependency>    
    <groupId>javax.servlet</groupId>    
    <artifactId>servlet-api</artifactId>    
    <version>3.0-alpha-1</version>    
</dependency>  
      
<dependency>  
    <groupId>javax.servlet</groupId>  
    <artifactId>jstl</artifactId>  
    <version>1.2</version>  
</dependency>  
      
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.4</version>
</dependency>
      
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>
  
  
  </dependencies>
  
  <build>
    <finalName>SpringMVCFileUpload</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
          
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>


Step 3: The web.xml file in the WEB-INF folder defines mapping with different URLs and servlets to handle requests for those URLs. In this configuration file, we have used listener for application startup, configured servlet, and added a servlet-mapping to map the URL.

Configuration File: web.xml

XML




    version="3.0">
  
    <display-name>To do List</display-name>
  
    <welcome-file-list>
        <welcome-file>login.do</welcome-file>
    </welcome-file-list>
      
    <servlet>
        <servlet-name>gfg</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/gfg-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
  
    <servlet-mapping>
        <servlet-name>gfg</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>


File: gfg-servlet.xml

This is the gfg-servlet.xml file located in  “/src/main/webapp/WEB-INF/gfg.servlet.xml“. This file handles all HTTP requests for web applications. The annotation-driven enable the spring @Controller function, resource-mapping helps in handling HTTP requests for all resources. The bean configuration helps in identifying and scanning the jsp located in the views folder. The component-scan locates and allocated beans according to the mentioned annotation. Also added a resource mapping to map all the resources to the view a page.

Step 4: A bean with id as multipartResolver will help in instantiating the CommonsMultipartResolver.

XML




  
    <context:component-scan base-package="com.gfg" />
      
    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" cache-period="31556926"/>
      
    <mvc:annotation-driven />
      
     <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        name="viewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
      
    <bean id="multipartResolver"   
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>  
          
      
      
</beans>


Step 7: The UploadFileController class in the com.gfg.controller has was methods for two requests to be mapped. The upload method is a get mapping and simple redirects to the fileform.jsp view page. The fileUpload method sends a Post request and redirects the showupload page. This class has three parameters CommonsMultipartFile gets the uploaded file. The file is converted into a bytes array and saved into a file using FileOutputStream, the model param is used to add the file name as an attribute in the showupload.jsp file.

Example

Java




// Java Program to Illustrate UploadFileController Class
  
package com.gfg.controller;
  
// Importing required classes
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
  
// Annotation
@Controller
// Class
public class UploadFileController {
  
    @GetMapping("/upload") public String upload()
    {
        return "fileform";
    }
  
    @RequestMapping(value = "/uploadfile",
                    method = RequestMethod.POST)
    public String
    fileUpload(@RequestParam("thisfile")
               CommonsMultipartFile file, HttpSession s,
               Model mod)
    {
  
        // Getting bytes of file and
        // storing it in a byte array
        byte[] data = file.getBytes();
  
        String filePath
            = s.getServletContext().getRealPath("/")
              + "WEB-INF" + File.separator + "resources"
              + File.separator + "image" + File.separator
              + file.getOriginalFilename();
  
        // Try block to check for exceptions
        try {
  
            // Creating an object of FileOutputStream class
            FileOutputStream fileout
                = new FileOutputStream(filePath);
            fileout.write(data);
  
            // Closing connections of file
            // using close() method
            fileout.close();
            mod.addAttribute("imgName",
                             file.getOriginalFilename());
        }
  
        // Catch block to handle the exceptions
        catch (Exception e) {
  
            // Displaying the exception/s along with
            // line number using printStackTrace() method
            e.printStackTrace();
        }
  
        return "showupload";
    }
}


Step 8: The fileform.jsp in the views folder defines the upload form with enctype as multipart/form-data. We’ve used bootstrap for the proper styling of the page.

Example

HTML




<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  
    <title>File uploader</title>
  </head>
  <body>
    <h1>Upload File</h1>
    <form action="uploadfile" method="post" enctype="multipart/form-data">
        <div class="form-group">
          <label for="formFile" class="form-label">Upload Your file</label>
          <input name="thisfile" class="form-control" type="file" id="formFile">
        </div>
        <button class="btn btn-secondary">Upload</button>
    </form>
  
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>


The showupload.jsp page displays the uploaded image using jsp to map the image url.

HTML




<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" url="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
  
    <h1>File Uploaded</h1>
  
    <img src="<c:url value="resources/image/${imgName}"/>"/>
</body>
</html>


After adding all the classes and configuration file, the project structure should look something like this:

Step 9: Now it’s time to run your project, start the Tomcat Server and run your application, now type “http://localhost:8080/SpringMVCFileUpload/upload” in any browser.

Output: 

Upload the image and click on upload this will redirect you to the showupload page and you will see your uploaded image.

So we have created a Spring MVC web application with an upload form and displayed the uploaded image on the web.



Last Updated : 22 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads