【转】HTTP Response Header 的 Content-Disposition
因為聽到有同事討論JSP輸出Excel文件的,就是在頁面上有一個【導出】按鈕,能夠將查詢結果導出到Excel文件讓用戶下載。有人說要用POI在后臺生成臨時的Excel文件,然后通過讀取FileStream寫入到OutputStream來解決。其實這個功能不需要這么重型的武器的,雖然很多人討厭MS,但是不得不承認MS絕對不是亂蓋的,IE和Office產品的幾近完美的結合就是一個列子。頁面里面的Table很容易就可以導出到Excel文件,而且格式都能夠完好的保存,所以如果要將查詢結果導出到Excel,只需將頁面的Context-Type修改一下就可以了:
<%@ page language="java" contentType="application/vnd.ms-excel"%>
然后請求這個頁面的時候,就會出現這樣的IE窗口:
?
看到上面的菜單了么?除了IE的,還有Excel的。而且在點擊“文件”->“另存為”的時候,如果選擇“Excel工作簿”,那么保存的文件就是真正的轉換到Excel文件的存儲格式了。
?
不過,有一個問題是,如果我不希望直接在IE里面打開Excel文件,我希望能夠提供那個打開/保存的對話框,應該如何做?
模模糊糊記得很久很久以前用過一個參數,于是乎,google一下吧,找到了,就是這個Content-Disposition參數,HTTP Response Header的一個參數。但是這個不是標準參數,查看一下HTTP/1.1的規范文檔,對于這個參數的解釋大意如下:
Content-Disposition參數本來是為了在客戶端另存文件時提供一個建議的文件名,但是考慮到安全的原因,就從規范中去掉了這個參數。但是由于很多瀏覽器已經能夠支持這個參數,所以只是在規范文檔中列出,但是要注意這個不是HTTP/1.1的標準參數。
于是,在頁面加入一行代碼:
<%
response.addHeader("Content-Disposition", "attachment;filename=test.xls");
%>
?
能夠看到“文件下載”的對話框了:
?
測試了一下,其實IE是根據Content-Disposition中filename這個段中文件名的后綴來識別這個文件類型的,所以,如果有很多種文件類型的時候,可以將Content-Type設置為二進制模式的:
<%@ page language="java" contentType="application/octet-stream"%>
?
附Content-Disposition的解釋:
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 receiving user agent SHOULD 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 SHOULD 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 should not display the response, but directly enter a `save response as...' dialog.
?
Content-Disposition中文亂碼
Response.setHeader(”Content-Disposition”, “attachment; filename=” + fileName+”.xls”);
如果file.Name為中文則亂碼。解決辦法是
方法1:
response.setHeader(”Content-Disposition”, “attachment; filename=” + java.net.URLEncoder.encode(fileName, “UTF-8″));
下載的程序里有了上面一句,一般在IE6的下載提示框上將正確顯示文件的名字,無論是簡體中文,還是日文。但是文字只要超過17個字,就不能下載了。
一. 通過原來的方式,也就是先用URLEncoder編碼,當中文文字超過17個時,IE6 無法下載文件。這是IE的bug,參見微軟的知識庫文章 KB816868 。原因可能是IE在處理 Response Header 的時候,對header的長度限制在150字節左右。而一個漢字編碼成UTF-8是9個字節,那么17個字便是153個字節,所以會報錯。而且不跟后綴也不對.
方法2:
response.setHeader( “Content-Disposition”, “attachment;filename=” + new String( fileName.getBytes(”gb2312″), “ISO8859-1″ ) );
在確保附件文件名都是簡體中文字的情況下,那么這個辦法確實是最有效的,不用讓客戶逐個的升級IE。如果臺灣同胞用,把gb2312改成big5就行。但現在的系統通常都加入了國際化的支持,普遍使用UTF-8。如果文件名中又有簡體中文字,又有繁體中文,還有日文。那么亂碼便產生了。另外,在上Firefox (v1.0-en)下載也是亂碼。
?
本文我們來說一下MIME 協議的一個擴展Content-disposition。
??? 我們在開發web系統時有時會有以下需求:
- 希望某類或者某已知MIME 類型的文件(比如:*.gif;*.txt;*.htm)能夠在訪問時彈出“文件下載”對話框
- 希望以原始文件名(上傳時的文件名,例如:山東省政府1024號文件.doc)提供下載,但服務器上保存的地址卻是其他文件名(如:12519810948091234_asdf.doc)
- 希望某文件直接在瀏覽器上顯示而不是彈出文件下載對話框
- ……………………
??? 要解決上述需求就可以使用Content-disposition來解決。第一個需求的解決辦法是
Response.AddHeader "content-disposition","attachment; filename=fname.ext" 將上述需求進行歸我給出如下例子代碼:public static void ToDownload(string serverfilpath,string filename)
{
FileStream fileStream = new FileStream(serverfilpath, FileMode.Open);
long fileSize = fileStream.Length;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + UTF_FileName(filename) + "\";");
attachment --- 作為附件下載
inline --- 在線打開
HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
byte[] fileBuffer = new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
HttpContext.Current.Response.BinaryWrite(fileBuffer);
fileStream.Close();
HttpContext.Current.Response.End();
}
public static void ToOpen(string serverfilpath, string filename)
{
FileStream fileStream = new FileStream(serverfilpath, FileMode.Open);
long fileSize = fileStream.Length;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=\"" + UTF_FileName(filename) + "\";");
HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
byte[] fileBuffer = new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
HttpContext.Current.Response.BinaryWrite(fileBuffer);
fileStream.Close();
HttpContext.Current.Response.End();
}
private static string UTF_FileName(string filename)
{
return HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);
}
?
簡單的對上述代碼做一下解析,ToDownload方法為將一個服務器上的文件(serverfilpath為服務器上的物理地址),以某文件名 (filename)在瀏覽器上彈出“文件下載”對話框,而ToOpen是將服務器上的某文件以某文件名在瀏覽器中顯示/打開的。注意其中我使用了 UTF_FileName方法,該方法很簡單,主要為了解決包含非英文/數字名稱的問題,比如說文件名為“衣明志.doc”,使用該方法客戶端就不會出現 亂碼了。
需要注意以下幾個問題:
轉載于:https://www.cnblogs.com/vinozly/p/4915374.html
總結
以上是生活随笔為你收集整理的【转】HTTP Response Header 的 Content-Disposition的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 教你如何更改xshell中的转发规则
- 下一篇: DOM优化