How to Implement File Upload in Spring Boot: Step-by-Step Guide

This tutorial explains how to add file‑upload functionality to a Spring Boot application, covering project setup, template engine configuration, HTML upload page creation, controller implementation, property settings, testing steps, and where to find the full source code.

Programmer DD
Programmer DD
Programmer DD
How to Implement File Upload in Spring Boot: Step-by-Step Guide

File upload is a common scenario in web applications, such as avatar uploads or Excel data import, requiring an upload step before further processing.

This article walks through implementing file upload in a Spring Boot project.

Step 1: Create a basic Spring Boot project

Initialize a Spring Boot application (see quick start guide if needed).

Step 2: Add template engine dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

You may also use other template engines such as Freemarker.

Step 3: Create upload page

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title>File Upload Page</title>
</head>
<body>
    <h1>File Upload Page</h1>
    <form method="post" action="/upload" enctype="multipart/form-data">
        Choose file: <input type="file" name="file"><br>
        <hr>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

Step 4: Implement UploadController

@Slf4j
@Controller
public class UploadController {

    @Value("${file.upload.path}")
    private String path;

    @GetMapping("/")
    public String uploadPage() {
        return "upload";
    }

    @PostMapping("/upload")
    @ResponseBody
    public String create(@RequestPart MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String filePath = path + fileName;

        File dest = new File(filePath);
        Files.copy(file.getInputStream(), dest.toPath());
        return "Upload file success : " + dest.getAbsolutePath();
    }
}

Key elements:

Member variable path injected from file.upload.path configuration.

GET / returns the upload page.

POST /upload saves the uploaded file to the configured directory.

Note: This demo shows the basic flow; real applications should handle filename conflicts, distributed storage, etc.

Step 5: Configure application.properties

spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=2MB

file.upload.path=/Users/didi/

The first two properties limit upload size; the last defines the directory for saved files.

Testing

1. Run the Spring Boot application and visit http://localhost:8080 to see the upload page.

Upload page screenshot
Upload page screenshot

2. Choose a file ≤2 MB and click “Submit”. If successful, a page showing the file path appears.

Upload success screenshot
Upload success screenshot

You can verify the file was saved at the printed path.

Code Repository

Examples are available in the chapter4-3 directory of the following repositories:

GitHub: https://github.com/dyc87112/SpringBoot-Learning/

Gitee: https://gitee.com/didispace/SpringBoot-Learning/

If you find this tutorial helpful, please star the repository.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javabackend-developmentSpring Boot
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.