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

Video

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="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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.javainuse</groupId>
	<artifactId>spring-boot-fileupload</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.1.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>
		




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

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

@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;

@Controller
public class FileUploadController {

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

	@RequestMapping("/upload")
	public ModelAndView showUpload() {
		return new ModelAndView("upload");
	}

	@PostMapping("/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) {
			e.printStackTrace();
		}

		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.
spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.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
	
<html>
<body>

	<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" />
	</form>

</body>
</html>


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

Status:  

</body>
</html>
Compile and the run the SpringBootFileUploadHelloWorldApplication.java 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