InputStreamResource is a Spring Resource that wraps an already opened InputStream, so a controller can return it as a response body and Spring copies the bytes to the client in small chunks. The file is never loaded into memory as a byte[], which makes it a good fit for large downloads.
It has two catches. The stream can be read only once, and Spring cannot work out the content length from it, so you should set Content-Length and Content-Disposition yourself. For a file on disk, FileSystemResource or PathResource is usually simpler.
org.springframework.core.io.InputStreamResource implements Resource for a given InputStream. Spring's ResourceHttpMessageConverter writes any Resource to the response, reading and writing a buffer at a time.
Its behaviour differs from file-based resources in a few ways:
isOpen() returns true, signalling that the stream must be read and closed once.getInputStream() throws IllegalStateException on a second call.exists() always returns true, even if the underlying source is missing, so check before you build the resource.getFilename() returns null and it has no File or length of its own.This controller streams a file from disk with the correct headers.
The code checks that the resolved path stays inside the storage folder, which blocks path traversal such as ../../etc/passwd. It then opens the stream, sets the content length from Files.size, and returns the resource. Spring closes the stream after writing the response.
For an InputStreamResource, Spring deliberately does not call contentLength(), because the default implementation would read the whole stream to count the bytes and then leave nothing to send. If you omit the header the response uses chunked transfer encoding. That works, but browsers cannot show a progress bar or resume a download, so provide the size whenever you know it.
If clients need resumable downloads, return a resource that supports ranges. FileSystemResource or PathResource combined with ResponseEntity lets Spring MVC honour Range headers through ResourceRegion support, which an InputStreamResource cannot provide because it cannot be re-read.
When the content is produced on the fly, such as a large CSV export from a database cursor, StreamingResponseBody gives direct access to the output stream.
This runs on an asynchronous thread, so configure the MVC async task executor for production use. Prefer virtual threads on Java 21 or later if your Spring Boot version enables them.
| Resource | Source | Length known | Re-readable | Range support |
|---|---|---|---|---|
FileSystemResource | File on disk | Yes | Yes | Yes |
PathResource | java.nio.file.Path | Yes | Yes | Yes |
ByteArrayResource | byte[] in memory | Yes | Yes | Yes |
InputStreamResource | Any open stream | No | No | No |
Use InputStreamResource for data that is only available as a stream, for example an object from cloud storage or a decrypted stream. Use a file-based resource when the file is local.
IllegalStateException.try-with-resources in the controller closes it before Spring writes the response. Let the framework close it.ContentDisposition.InputStreamResource load the whole file in memory?No. It copies from the stream to the response in small buffers, so memory use stays flat.
The Content-Length header is probably missing. Set it with contentLength(...) when the size is known.
InputStreamResource suitable for WebFlux?Spring WebFlux can write a Resource, but a reactive application should prefer DataBuffer streams or FileSystemResource to avoid blocking threads.
InputStreamResource for local files?Usually not. FileSystemResource supports length, range requests and repeated reads without extra code.
InputStreamResource streams data from an InputStream to the HTTP response without buffering it all in memory. Set Content-Length and Content-Disposition explicitly, validate paths and permissions, and remember the stream is single-use. Choose FileSystemResource for local files and StreamingResponseBody for generated output.