File Upload and Download in Spring Boot — MultipartFile and Streaming
Fast recall
Quick revision
Core answer
Whether the candidate can implement both upload and download with proper headers and size limits.
Loading the entire file into a byte[] for download instead of streaming with InputStreamResource, causing OutOfMemoryError on large files.
Mention streaming for large files, configuring max-file-size and max-request-size, and storing files externally (S3) instead of on the local filesystem.
Interview-ready guide
Interview answer
File upload and download are common requirements with non-obvious memory management concerns. The wrong approach loads entire files into JVM heap — a denial-of-service risk with large files.
Upload
- Accept
MultipartFilein the controller:@RequestParam("file") MultipartFile file file.getInputStream()streams the upload content; for small filesfile.getBytes()reads all bytes into memory — be careful with themax-file-sizelimit- Set
spring.servlet.multipart.max-file-size=50MBandmax-request-size=55MBinapplication.properties; also configure at the API gateway or load balancer
Download
- Return
ResponseEntitywithInputStreamResourcewrapping a file input stream — Spring streams to the client - Or return
StreamingResponseBodyfor full control over chunked writing; runs in a separate thread, releasing the Tomcat worker thread - Always set
Content-Disposition: attachment; filename="filename.pdf"andContent-Typeheaders
S3 pre-signed URLs — the production pattern for large files
- Application generates a pre-signed URL with a short TTL (5-15 minutes)
- Client uploads directly to S3 or downloads directly from S3 — application never handles the bytes
- Eliminates egress bandwidth cost on the application tier and scales to any file size
File Upload and Download in Spring Boot — MultipartFile and Streaming
Direct Answer
File upload uses MultipartFile as a controller parameter with @RequestParam. File download uses ResponseEntity<Resource> with appropriate Content-Disposition headers. For large files, streaming with InputStreamResource avoids loading the entire file into memory. Spring Boot's multipart configuration controls max file size and total request size.
Common Mistakes & Pitfalls
Loading the entire file into a byte[] for download instead of streaming with InputStreamResource, causing OutOfMemoryError on large files.
Check your understanding
Answer these without looking back at the article.
- What are the trade-offs when applying File Upload and Download in Spring Boot — MultipartFile and in production?
- What are the trade-offs when applying File Upload and Download in Spring Boot — MultipartFile and in production?
- What are the trade-offs when applying File Upload and Download in Spring Boot — MultipartFile and in production?
Related interview prep categories
1Want a guided curriculum order?
These modules are also sequenced in our flagship backend interview track — open it when you prefer an ordered path instead of browsing by topic.