VOOZH about

URL: https://www.geeksforgeeks.org/springboot/spring-mvc-file-upload/

⇱ Spring MVC File Upload - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring MVC File Upload

Last Updated : 27 Aug, 2025

Spring MVC provides built-in multipart handling via the Jakarta Servlet API, so you can upload files using MultipartFile and StandardServletMultipartResolver. This article walks through enabling multipart in web.xml (or Java config), registering StandardServletMultipartResolver, building a controller to accept uploads, and managing files efficiently (images, documents, large files) with limits, validation, and storage options.

Pre-requisites:

  • Eclipse IDE (or any other IDE of your choice)
  • Apache Maven for dependency management
  • Java 11 or higher
  • Apache Tomcat 10 or higher (for deploying the application)

Steps to Create a Spring MVC File Uploading Project

Spring MVC framework provides 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 configuring MultipartResolver to upload files (image) and also show them on the web.

Step 1: Create a Maven Web Application Project

Open Eclipse IDE and create a new Maven project. Select the maven-archetype-webapp archetype. Enter the Group Id (e.g., com.gfg) and Artifact Id (e.g., SpringMVCFileUpload). Click Finish to create the project.

Step 2: Project Structure

The project structure would look something like this:

👁 ProjectStructure

Step 3: Add Dependencies in pom.xml

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. We are going to add dependencies for Spring MVC, jakarta EE and file upload libraries.

pom.xml:

Step 4: Configure web.xml

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 Jakarta EE namespace and configure the DispatcherServlet.

web.xml:

Step 5: Configure 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.

gfg-servlet.xml:

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

Step 6: Create the Controller

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.

UploadFileController:

Step 7: Create JSP Views

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.

fileform.jsp:

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

showupload.jsp:

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

👁 ProjectStructure

Note: Before running the application, you need to manually create a folder named image inside the WEB-INF/resources directory. This folder will be used to store the uploaded files (e.g., images). If the folder does not exist, the application will throw an error when trying to save the uploaded file.

Step 8: Run the Application

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: 

The below image demonstrates a file upload form in a Spring MVC application where users can select and upload a file.

👁 RunTheApplication

Upload the image and click on upload this will redirect you to the showupload page

👁 ChooseFile

Now, you will see your uploaded image.

👁 FileUploaded
Output

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

Comment

Explore