The Content-Disposition HTTP response header tells the browser whether to show a response body inline in the page or save it as a downloaded file, and it can suggest a file name. Content-Disposition: attachment; filename="report.pdf" forces a download, while inline lets the browser display the content.
The header is defined for HTTP in RFC 6266 and its parameter encoding in RFC 8187. In Java it is most often set in Spring MVC with ContentDisposition and ResponseEntity, or in Jakarta Servlet with HttpServletResponse.setHeader.
| Part | Meaning |
|---|---|
inline | Display the body in the browser if it can render the type (default for most responses) |
attachment | Save the body as a file, usually with a download prompt |
filename | Suggested file name in ASCII, quoted |
filename* | Same, with an explicit character set and percent encoding (RFC 8187), for non-ASCII names |
The header is only a suggestion. The browser decides the final behaviour and sanitises the file name, for example by removing path separators. A correct Content-Type should always accompany it.
Use the ContentDisposition builder, which handles quoting and encoding for you.
The startsWith(ROOT) check blocks path traversal such as ../../etc/passwd. Never build the path straight from user input. With a UTF-8 charset the builder emits both filename and filename*, so non-ASCII names work in modern browsers.
Use inline to preview a PDF or image in the browser tab.
Whether the browser really displays it depends on the type: a PDF or image usually renders, a ZIP file still downloads.
Without Spring, set the header on the response and stream the bytes.
On older Java EE containers the servlet types live in javax.servlet instead of jakarta.servlet.
Outside responses, the same name appears in multipart/form-data requests, where each part carries Content-Disposition: form-data; name="file"; filename="photo.jpg". Spring exposes this through MultipartFile.getOriginalFilename(). Treat that name as untrusted input and never use it directly as a path.
attachment for user-uploaded content that could contain scripts, such as HTML or SVG, and add X-Content-Type-Options: nosniff.ContentDisposition do this.fetch calls the browser hides this header from JavaScript unless the server also sends Access-Control-Expose-Headers: Content-Disposition.download attribute on an <a> element can suggest a download for same-origin links, but the server header is the reliable method.inline asks the browser to show the content in the page when it supports the type. attachment asks it to save the content as a file.
Send the filename* parameter with UTF-8 percent encoding. In Spring, call filename(name, StandardCharsets.UTF_8) on the ContentDisposition builder.
Common reasons are a missing or malformed quote, an invalid character, a conflicting Content-Type, or a proxy rewriting headers. The browser also replaces characters that are illegal in file names.
Only for same-origin requests, or cross-origin when the server exposes it with Access-Control-Expose-Headers.
Content-Disposition controls whether a response is displayed (inline) or saved (attachment) and suggests a file name. In Java, build it with Spring's ContentDisposition, always send a correct Content-Type, encode non-ASCII names with filename*, and validate file paths and names to avoid traversal and header injection.