File download means the server finds a stored file and sends it back to the client as the HTTP response, with headers that tell the client the file name, type, and size. The Content-Disposition header decides whether the browser saves the file (attachment) or opens it (inline). It is used for downloading certificates, marksheets, invoices, and reports that were stored earlier, so users get the real file instead of JSON. In this example, student documents are kept in a documents folder, their details are in MySQL, and the API can download one document, view it, or download all of them as one ZIP.
Features
- Finds the file through its database record and reads it from the
documentsfolder - Correct
Content-Type,Content-Length, and file name headers - Download (save) and view (open in browser) from the same stored file
- Download all documents of a student as one ZIP, streamed without building the ZIP in memory
- Checks that a document belongs to the student in the URL (otherwise 404)
- Returns 404 when the student, the document, or the file on disk is missing
- Stored UUID file names are hidden: the client receives the original file name.
File Download Types
- Attachment download:
Content-Disposition: attachmentmakes the browser save the file. The/downloadendpoint does this. - Inline view:
Content-Disposition: inlinemakes the browser open the file, for example a PDF in its viewer or an image. The/viewendpoint does this. - Resource response: the controller returns
ResponseEntity<Resource>with aFileSystemResource. This is the normal way to return one stored file, and it also lets clients use HTTP Range requests for partial or resumed downloads. - ZIP download of multiple files: several files are packed into one ZIP. The
/download-allendpoint does this. - Streaming download:
StreamingResponseBodywrites data to the response while it is produced, so large responses such as a ZIP never have to fit in memory. The ZIP endpoint uses it.
