Spring Boot File Upload- Hello World Example | JavaInUse

Spring Boot File Upload- Hello World Example

In this post we implement a simple example to upload Multipart file using Spring Boot. The following gives us an overview of the operations we will be performing.


This tutorial is explained in the below Youtube Video.

Lets Begin-

Maven project will be as follows -

Along with the Spring Boot dependencies we have also tomcat-embed-jasper and jstl jar dependencies as these required when jsp view is used. In the pom file add the following dependencies-
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">


		<relativePath /> <!-- lookup parent from repository -->





Create the SpringBootFileUploadHelloWorldApplication.java as below-
package com.javainuse;

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

public class SpringBootFileUploadHelloWorldApplication {

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

Next we define the FileUploadController.
@RequestMapping maps /upload request to showUpload() method and returns the upload.jsp view to be displayed. Spring has built-in multipart support for file uploads in web applications.
package com.javainuse.controllers;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

public class FileUploadController {

	//destination folder to upload the files
	private static String UPLOAD_FOLDER = "C://test//";

	public ModelAndView showUpload() {
		return new ModelAndView("upload");

	public ModelAndView fileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {

		if (file.isEmpty()) {
			return new ModelAndView("status", "message", "Please select a file and try again");

		try {
			// read and write the file to the selected location-
			byte[] bytes = file.getBytes();
			Path path = Paths.get(UPLOAD_FOLDER + file.getOriginalFilename());
			Files.write(path, bytes);

		} catch (IOException e) {

		return new ModelAndView("status", "message", "File Uploaded sucessfully");


In the application.properties we define the prefix and suffix as follows. So in the above controller the /upload correctly maps to /WEB-INF/jsp/upload.jsp.

Finally we define the jsp pages-
The upload.jsp allows the user to select the file to be uploaded and submit it. On submit the fileUpload() method is called using POST and the selected file is processed. When sending a file, it is necessary to tell the HTTP protocol that you are sending a file having several characteristics and information inside it.
enctype='multipart/form-data' means that no characters will be encoded. that is why this type is used while uploading files to server. So multipart/form-data is used when a form requires binary data, like the contents of a file, to be uploaded

	<h1>Spring Boot - File Upload</h1>

	<form method="POST" action="/upload" enctype="multipart/form-data">
		<input type="file" name="file" /><br />
		<br /> <input type="submit" value="Submit" />


The status.jsp displays the File upload status when user clicks on submit.
<h1>File Upload Status</h1>


Compile and the run the SpringBootFileUploadHelloWorldApplication.java as a Java application.
Go to localhost:8080/upload

Select the file to be uploaded-

On clicking Submit the file will be uploaded-

If the user clicks submit without selecting any file, he will see the following message-

Download Source Code

Download it -
Spring Boot File Upload Application

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Gradle Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions