Skip to content
Ameen Abozaid
Planned lessonJNR-023-L03

Streaming downloads

  • Lesson 3 of 6
  • Junior
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

send big files without loading them into memory.

Planned topics

  • Reading a whole file into a byte[] uses memory per request; ten parallel downloads can crash the server.
  • Return a Resource (for example FileSystemResource) or a StreamingResponseBody; Spring copies it in chunks.
  • Set Content-Type from your stored record, and Content-Length when known.
  • Content-Disposition: attachment; filename="..." makes browsers download; inline displays.
  • Return 404 if the file record or file is missing.
Draft outline details

Demonstration

java
@GetMapping("/images/{id}")
ResponseEntity<Resource> download(@PathVariable String id) {
    Resource r = storage.load(id);
    return ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(r);
}

Check your understanding

Why not return a byte[]?

Show answer

The whole file sits in memory for each request.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.