Content-Disposition=attachment header.- Author:
- Bruce Chapman, Sudhan Moghe, Daniel Gredler, Ronald Brill, Lai Quang Duong, Vidya m
-
Constructor Summary
ConstructorsConstructorDescriptionAttachment(Page page) Creates a new attachment for the specified page.Attachment(Page page, String attachmentFilename) Creates a new attachment for the specified page. -
Method Summary
Modifier and TypeMethodDescriptiongetPage()Returns the attached page.Returns the file name for this attachment, the way a real browser would suggest it in its file save dialog (or use it for an automatic download).static StringgetSuggestedFilename(String contentDispositionHeader) Returns the file name suggested by the givenContent-Dispositionheader, ornullif the header does not contain a usable file name.
-
Constructor Details
-
Attachment
Creates a new attachment for the specified page.- Parameters:
page- the attached page
-
Attachment
Creates a new attachment for the specified page.- Parameters:
page- the attached pageattachmentFilename- the file name of this attachment
-
-
Method Details
-
getPage
Returns the attached page.- Returns:
- the attached page
-
getSuggestedFilename
Returns the file name for this attachment, the way a real browser would suggest it in its file save dialog (or use it for an automatic download).If a file name was given explicitly when this attachment was created (for example because the download was triggered by an anchor with a
downloadattribute, or the name was already derived from the response by theWebClient), this name is returned as it is. Otherwise the name is taken from theContent-Dispositionheader of the response of the attached page; seegetSuggestedFilename(String)for the details.Like a browser, this method does not use the URL of the request as fallback. If you need a name in this case, you have to build one on your own (for example from the URL, as the
DownloadingAttachmentHandlerdoes).- Returns:
- the attachment's suggested filename, or
nullif none was suggested
-
getSuggestedFilename
Returns the file name suggested by the givenContent-Dispositionheader, ornullif the header does not contain a usable file name.This method does what the real browsers do when they choose the name of a downloaded file, so the result is a name that is safe to use for saving the file, and not the plain value sent by the server:
- The header is parsed by the
ContentDispositionParser(RFC 6266, RFC 2183). Parameter names are case-insensitive, the value can be a token or a quoted string (with escapes), and a;inside a quoted string does not end the value. - The extended parameter
filename*(RFC 8187, e.g.filename*=UTF-8''gr%C3%B6%C3%9Fe.txt) wins overfilename, whatever the order in the header is. Supported charsets are UTF-8 and ISO-8859-1; if the extended value is not usable, the plainfilenameis used. - Characters that are not allowed in file names on at least one platform
(
\ / : * ? " < > |and control characters) are replaced by an underscore (seeFileUtils.sanitizeFilename(String)). This also makes sure a path in the header can never be used to leave the download directory:../../evil.txtbecomes_.._evil.txt. - Leading dots and white space (no hidden files) and trailing dots and white space are removed.
- If nothing is left, for example because the header has no file name or an empty one,
the result is
null. A browser would use the last part of the URL in this case, this has to be done by the caller.
Not all browsers behave exactly the same in some corner cases. If the extended value uses an unknown charset, this method behaves like Chrome and Edge and ignores the extended value; Firefox decodes the value anyway. For an invalid percent encoding, Chrome and Edge keep the broken value as it is, while Firefox ignores the extended value; this method behaves like Firefox and falls back to the plain
filename.- Parameters:
contentDispositionHeader- the value of theContent-Dispositionheader, can benull- Returns:
- the sanitized file name, or
nullif none was suggested
- The header is parsed by the
-