The function and usage of response.setContentType() and Content-Disposition in the header

1. response.setContentType() :

1.1 Overview:

Content type, generally refers to the Content-Type existing in a web page, which is used to define the type of network file and the encoding of the web page, and determine the format and encoding of the browser to read this file.
Correspondence between file extension and Content-type, see: http://tool.oschina.net/commons

1.2 Common methods:

response.setContentType("text/html"); // the most popular one 
response.setContentType("text/plain");  
response.setContentType("text/css"); // Cascading Style Sheet 
response.setContentType("application/html"); 
response.setContentType("image/gif"); 
response.setContentType("application/zip"); 
response.setContentType("application/pdf");
//当浏览器在请求资源时,会通过http返回头中的content-type决定如何显示/处理将要加载的数据,如果这个类型浏览器能够支持阅览,浏览器就会直接展示该资源,比如png、jpeg、video等格式。在某些下载文件的场景中,服务端可能会返回文件流,并在返回头中带上Content-Type:application/octet-stream,告知浏览器这是一个字节流,浏览器处理字节流的默认方式就是下载。
Application/octet-stream是应用程序文件的默认值。意思是未知的应用程序文件,浏览器一般不会自动执行或询问执行。浏览器会像对待,设置了HTTP头Content-Disposition值为attachment的文件一样来对待这类文件,即浏览器会触发下载行为。
sponse.setContentType("application/octet-stream"); 

2. Content-Disposition:

2.1 Overview

It is an extension of the MIME protocol. The MIME protocol instructs the MIME user agent how to display the attached file. The response header indicates in what form the content of the reply should be displayed, whether it is inline (that is, a web page or part of a web page), or as an attachment. The form is downloaded and saved locally
. When Internet Explorer receives the headers, it activates the file download dialog, whose filename box is automatically filled with the filename specified by the headers.

When the server sends a file to the browser, if it is a file type supported by the browser, it will generally be opened by the browser by default, such as txt, jpg, etc. If you need to prompt the user to save, you must use Content-Disposition for processing. (Knock on the blackboard, draw the key point) The key is to add attachment [attachment].
For example:

Response.AppendHeader("ContentDisposition","attachment;filename=FileName.txt");

In this case, when the browser is opened, it will prompt to save or open. Even if you choose to open, it will use the associated program, such as Notepad, instead of IE to open directly.
Content-Disposition is to provide a default file name when the user wants to save the requested content as a file.
The specific definitions are as follows:

//content-disposition的定义
content-disposition ="Content-Disposition" ":"
                     disposition-type
                     *(";" disposition-param)
//disposition-type的定义
disposition-type="attachment"|disp-extension-token
//disposition-param的定义
disposition-param=filename-param|disp-extension-parm
//filename-param的定义
filename-param= "filename"  "=" quoted-string
//disp-extension-token的定义
disp-extension-token = token
//disp-extension-parm
token "=" ( token | quoted-string )

Common forms:

Content-Disposition: inline
Content-Disposition: attachment
Content-Disposition: attachment; filename="filename.jpg"

2.2 Example:

        fileName = URLEncoder.encode("导入模版.xlsx","utf-8");
        response.setHeader("Content-disposition","attachment; filename="+fileName);
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");

Note:
Of course, the filename parameter can contain path information, but User-Agnet will ignore this information, and only use the last part of the path information as the file name. If the above header information is used when the response type is application/octet-stream, then the content cannot be displayed directly, but a "file download" dialog box pops up, and then the user decides to "open" or "save". .

Guess you like

Origin blog.csdn.net/u014212540/article/details/130060043