Spring MVC Single File Upload Example

In this tutorial I am going to show you an example on how to upload a single file using Spring MVC. If you need to upload multiple files then you can go through Spring MVC Multiple Files Upload Example. I am using Spring Boot framework to implement single file upload example.

I am using Thymeleaf as a template file for displaying UI (User Interface). I have put basic validation for the file upload form. If you do not select any file for upload then you will see an error message.


Java at least 8, Spring Boot 2.3.3, Gradle 6.5.1, Maven 3.6.3

Project Setup

Either create gradle or maven based project in your favorite IDE or tool. The name of the project is spring-mvc-single-file-upload.

If you creating gradle based project then you can use below build.gradle script:

buildscript {
	ext {
		springBootVersion = '2.3.3.RELEASE'
    repositories {
    dependencies {

plugins {
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {

If you are creating maven based project then you can use below pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">





					<source>at least 8</source>
					<target>at least 8</target>

Spring Controller

Spring controller is responsible for handling any request and response for the clients. Here I have defined two endpoints in the following Spring controller class.

The root path (“/”) displays the file upload form or page where a browse button is given for selecting a file using browse button.

Other endpoint path (“/upload”) upload the selected file into server location.

package com.roytuts.spring.mvc.single.file.upload;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

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;

public class FileUploadController {

	private final String UPLOAD_FOLDER = "/upload";

	public String uploadFiles() {
		return "uploadfile";

	public String handleFilesUpload(@RequestParam("file") MultipartFile file, Model map) {
		StringBuilder sb = new StringBuilder();

		if (file != null) {
			try {
				if (!Files.exists(Paths.get(UPLOAD_FOLDER))) {
					try {
					} catch (IOException ioe) {

				Files.copy(file.getInputStream(), Paths.get(UPLOAD_FOLDER, file.getOriginalFilename()));
				sb.append("You successfully uploaded " + file.getOriginalFilename() + "!\n");

				map.addAttribute("msg", sb);
			} catch (IOException | RuntimeException e) {
				sb.append("Failued to upload " + (file != null ? file.getOriginalFilename() : "") + " => "
						+ e.getMessage() + String.valueOf(e) + "\n");

				map.addAttribute("msg", sb);
		} else {
			sb.append("Failed to upload file\n");
			map.addAttribute("msg", sb);

		return "uploadfile";


View File

The view file or Thymeleaf template file uploadfile.html is kept under the classpath directory src/main/resources/templates.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<title>Spring MVC Single File Upload Example</title>
	<h2>Spring MVC Single File Upload Example</h2>

	<div th:if="${msg != null}" th:text="${msg}"></div>

	<form name='upload_file' enctype="multipart/form-data"
		th:action="@{/upload}" method='POST'>
		Select Files: <input type="file" name="file" /> <br /> <br /> <input
			value="Upload" type="submit" />

Main Class

A class with @SpringBootApplication and main method is enough to deploy the application into embedded Tomcat server.

package com.roytuts.spring.mvc.single.file.upload;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringMvcSingleFileUploadApp {

	public static void main(String[] args) {
		SpringApplication.run(SpringMvcSingleFileUploadApp.class, args);


Testing the Application

Run the main class to deploy your application on Tomcat server.

When you hit the URL http://localhost:8080 in the browser, you will see below page in the browser.

spring mvc single file upload

When the file successfully uploaded you will see successful message:

spring mvc single file upload

Source Code


Thanks for reading.

Related posts

Leave a Comment