Download a file from Spring boot rest service
For those in a hurry, we can setup file downloads in Spring Boot using ResponseEntity<Resource>. Set the correct headers via HttpHeaders and use a FileSystemResource for the filepath. Define the content type as "application/octet-stream" in ResponseEntity for downloading binary data.
You've now got a /download endpoint, just hit it and the requested file named filename sprints to the browser.
Resource handling: Choose the right tool for the job
When dealing with file downloads, the correct resource selection is key. Both ByteArrayResource and InputStreamResource offer good performance, but alignment to the type of problem and file can optimize resource usage. That's like choosing the best utensil - Soup with fork? I don't think so!
For smaller files, ByteArrayResource has an edge with their better memory handling characteristics. On other hand, for streaming files without loading complete file in memory, InputStreamResource is often the choice.
| Resource Type | Preferred Usage |
|---|---|
| ByteArrayResource | Small Files |
| InputStreamResource | Streaming Files |
When dealing with IOExceptions, remember to always handle the try catch appropriately - Trapping exceptions is not just for Pokémon.
To stream large files without consuming excessive memory, StreamingResponseBody is your memory-friendly friend. Don’t forget to use a TaskExecutor for asynchronous handling of download request and to buffer your OutputStreams. Just like buffering a YouTube video, but for files.
Advanced use cases, tips & tricks
Direct file access with InputStream
To directly access a file's data stream:
Frontend downloads
While dealing with frontend downloads, remember to set the responseType to 'blob' on Axios calls. Libraries like js-file-download can also help to manage downloads well.
Serving large files
For streaming large files, use the StreamingResponseBody:
Handling potential landmines
Keep an eye on potential issues and handle them gracefully:
- File not found: Always validate existence of file beforehand.
- Client disconnections: Sudden client disconnections need management.
- Security: Put checks and controls in place for authorized access only.
Was this article helpful?