Build Restful File Upload & Download APIs with Spring Boot 2.2.6
This step‑by‑step tutorial explains how to create Restful file upload and download APIs using Spring Boot 2.2.6, covering environment setup, project structure, interface and implementation details, size‑limit configuration, global exception handling, and testing the endpoints with Postman.
File upload and download are common features in web applications. This tutorial shows how to implement Restful file upload/download APIs using Spring Boot 2.2.6 (Spring Web MVC only).
Part 1: Environment
JDK 1.8
Spring Boot 2.2.6 (Web MVC)
Maven 3.5+
IntelliJ IDEA 2019.2
Postman 7.23.0
Part 2: Features
Client uploads files to server.
Upload size limited to 50 MB.
Download files via link.
List uploaded files with name and download URL.
Part 3: Project Structure
config/FileUploadConfiguration.java – cleans old files on startup.
controller/FileUploadController.java – handles upload, download, list requests.
exception/FileUploadExceptionAdvice.java – global exception handling.
service/FileStorageService.java – interface for storage operations.
service/impl/FileStorageServiceImpl.java – implementation using java.nio.file.
valueobject/UploadFile.java – POJO for file name and URL.
valueobject/Message.java – POJO for response messages.
resources/application.yml (or application.properties) – configures max upload size.
pom.xml – Maven dependencies (spring-boot-starter-web).
Part 4: Creating the Spring Boot Project
Add the following Maven dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>Define the storage interface FileStorageService with methods init(), save(MultipartFile), load(String), load(), and clear(). Implement it in FileStorageServiceImpl using Files.createDirectory, Files.copy, UrlResource, and FileSystemUtils.deleteRecursively. The controller uses @RestController with @PostMapping("/upload"), @GetMapping("/files"), and @GetMapping("/files/{filename:.+}") to handle the respective operations.
Part 5: Configuring Upload Size
Set the maximum request and file size to 50 MB in application.yml or application.properties:
spring:
servlet:
multipart:
max-request-size: 50MB
max-file-size: 50MBPart 6: Global Exception Handling
Handle MaxUploadSizeExceededException with a @ControllerAdvice class that returns a friendly error message.
Part 7: Initializing Storage
Implement CommandLineRunner in FileUploadConfiguration to call clear() and init() at startup.
Part 8: Running and Testing
Start the application via mvn spring-boot:run, IDE run button, main method, or java -jar. After startup a fileStorage directory is created. Use Postman to test:
POST /upload to upload a file (files larger than 50 MB trigger the size‑limit exception).
GET /files to retrieve the list of uploaded files.
GET /files/{filename} to download a specific file.
Responses include appropriate HTTP headers for file download.
Conclusion
The tutorial demonstrates a complete Spring Boot 2.2.6 solution for Restful file upload and download, including size limitation, global error handling, and testing with Postman.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
