Content-Disposition of HTTP Response Header

Source: Internet
Author: User
When I heard a colleague discuss JSP Excel file output, there was an export button on the page to export the query results to an Excel file for users to download. Some people say that they want to use POI to generate a temporary Excel file in the background and then write it to OutputStream by reading FileStream. In fact, this function does not need such a heavy weapon. Although many people hate MS, they have to admit that MS is definitely not a mess. Almost perfect combination of IE and Office products is a column. The Table in the page can be easily exported to an Excel file, and the format can be well preserved. Therefore, if you want to export the query results to an Excel file, you only need to modify the Context-Type of the page:

<% @ Page language = "java" contentType = "application/vnd. ms-excel" %>

Then, the IE window will appear when you request this page:


Have you seen the menu above? IE and Excel are available. When you click "file"> "Save as", if you select "Excel Workbook", the saved file is actually converted to the storage format of the Excel file.


However, if I do not want to open an Excel file directly in IE, what should I do if I want to provide the dialog box for opening/saving?

I am confused. I have used a parameter a long time ago. So, google it and find it. It is the Content-Disposition parameter, a parameter of HTTP Response Header. However, this is not a standard parameter. Check the HTTP/1.1 standard document. The explanation of this parameter is as follows:

The Content-Disposition parameter is intended to provide a recommended file name when another file is stored on the client, but this parameter is removed from the specification for security reasons. However, because many browsers support this parameter, it is only listed in the standard documentation. However, note that this parameter is not a standard parameter of HTTP/1.1.

Therefore, add a line of code to the page:

Response. addHeader ("Content-Disposition", "attachment?filename=test.xls ");


The "File Download" dialog box is displayed:


After testing, IE identifies the file type based on the filename Suffix in the Content-Disposition segment. Therefore, if there are many file types, you can set Content-Type to binary:

<% @ Page language = "java" contentType = "application/octet-stream" %>


Appendix Content-Disposition explanation:

19.5.1 Content-Disposition The Content-Disposition response-header field has been proposed as a means for the origin server to suggest a default filename if the user requests that the content is saved to a file. this usage is derived from the definition of Content-Disposition in RFC 1806 [35].

Content-disposition = "Content-Disposition": "disposition-type * ("; "disposition-parm)
Disposition-type = "attachment" | disp-extension-token
Disposition-parm = filename-parm | disp-extension-parm
Filename-parm = "filename" "=" quoted-string
Disp-extension-token = token
Disp-extension-parm = token "=" (token | quoted-string)

An example is
Content-Disposition: attachment; filename = "fname. ext"


The specified ing user agent shocould NOT respect any directory path information present in the filename-parm parameter, which is the only parameter believed to apply to HTTP implementations at this time. the filename shoshould be treated as a terminal component only.

If this header is used in a response with the application/octet-stream content-type, the implied suggestion is that the user agent shocould not display the response, but directly enter a 'save response... 'Dialog.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.