Search Tutorials

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.
Spring Boot File Upload Setup


This tutorial is explained in the below Youtube Video.

Lets Begin-

Maven project will be as follows -
Spring Boot File Upload Example

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="" xmlns:xsi=""


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





Create the 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) {, 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.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;

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 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 as a Java application.
Go to localhost:8080/upload
Spring Boot File Upload Tutorial
Select the file to be uploaded-
Spring Boot File Upload
On clicking Submit the file will be uploaded-
Spring Boot File Upload Submit
If the user clicks submit without selecting any file, he will see the following message-
Spring Boot File Upload 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