Spring Boot File Upload Example | Code Factory
2 min readApr 3, 2020
Reference Link : Link
Donate : Link
SpringBootFileUploadApplication.java
package com.codeFactory;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringBootFileUploadApplication {public static void main(String[] args) {
SpringApplication.run(SpringBootFileUploadApplication.class, args);
}}
FileExceptionHandler.java
package com.codeFactory.controller;import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;@ControllerAdvice
public class FileExceptionHandler {//Spring 4.3.5 supports RedirectAttributes
@ExceptionHandler(MultipartException.class)
public String handleError1(MultipartException e, RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
return "redirect:/uploadStatus";
}
/* Spring < 4.3.5
@ExceptionHandler(MultipartException.class)
public String handleError2(MultipartException e) {
return "redirect:/errorPage";
}*/
}
FileUploadController.java
package com.codeFactory.controller;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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;@Controller
public class FileUploadController {private static String UPLOADED_FOLDER = "R://fileUpload//";@GetMapping("/")
public String index() {
return "upload";
}@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {if (file.isEmpty()) {
redirectAttributes.addFlashAttribute("message", "Please select a file.");
return "redirect:uploadStatus";
}try {
byte[] bytes = file.getBytes();
Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
Files.write(path, bytes);redirectAttributes.addFlashAttribute("message", "Successfully uploaded file : '" + file.getOriginalFilename() + "'");
} catch (IOException e) {
e.printStackTrace();
}return "redirect:/uploadStatus";
}@GetMapping("/uploadStatus")
public String uploadStatus() {
return "uploadStatus";
}
}
application.properties
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp#search multipart
spring.http.multipart.max-file-size=1MB
spring.http.multipart.max-request-size=1MB
upload.jsp
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h1>Spring Boot file upload example</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>
uploadStatus.jsp
<!DOCTYPE html>
<html>
<body><h1>File Upload Status</h1><h2>${message}</h2></body>
</html>
pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.codeFactory</groupId>
<artifactId>SpringBootFileUpload</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBootFileUpload</name>
<description>Spring Boot File Upload Example</description><properties>
<java.version>1.8</java.version>
</properties><dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies><build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build></project>