随笔 - 42  文章 - 71  trackbacks - 0
<2008年4月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

常用链接

留言簿

随笔档案

文章分类

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

因为听到有同事讨论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窗口:

image

 

看到上面的菜单了么?出了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");
%>

 

能够看到“文件下载”的对话框了:

image

 

测试了一下,其实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.

posted on 2008-04-23 18:23 YODA 阅读(11815) 评论(4)  编辑  收藏

FeedBack:
# re: HTTP Response Header 的 Content-Disposition 2008-04-23 19:08 BeanSoft
.... 还有些人不用 Windows 或者 Office 的。呵呵,虽然这样也可以。  回复  更多评论
  
# re: HTTP Response Header 的 Content-Disposition 2008-04-26 09:45 隔叶黄莺
比如有些报表,记录多的情况,确实不希望在浏览器中打开那个Excel,就应该这么做的。

楼主还有一个地方要注意的,如果要指定下载文件名是中文的话,可能就需要对那个文件名字符串编码成 ISO8859-1  回复  更多评论
  
# re: HTTP Response Header 的 Content-Disposition 2008-04-27 21:48 YODA
多谢楼上两位的补充  回复  更多评论
  
# re: HTTP Response Header 的 Content-Disposition 2009-06-06 10:18 阿里哇
@隔叶黄莺
好的,非常感谢。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: