1.在进行Web开发时,可能遇到遇到如下几种需求: 浏览器
l 但愿某类或者某已知MIME 类型的文件(好比:*.gif;*.txt;*.htm)可以在访问时弹出“文件下载”对话框。 app
l 但愿客户端下载时以指定文件名显示。 jsp
l 但愿某文件直接在浏览器上显示而不是弹出文件下载对话框。 spa
对于上面的需求,使用Content-Disposition属性就能够解决。下面是代码示例: code
response.setHeader("Content-disposition", "attachment;filename=" + fileName)。 htm
//Content-disposition为属性名。 开发
//attachment表示以附件方式下载。若是要在页面中打开,则改成inline。 get
//filename若是为中文,则会出现乱码。解决办法有两种: it
//1、使用fileName = new String(fileName.getBytes(), "ISO8859-1")语句 io
//2、使用fileName = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)语句
2.<%
//浏览器有此限制是不可以使用
//response.setHeader("Pragma", "No-cache");
//response.setHeader("Cache-Control", "No-cache");
//response.setDateHeader("Expires", 0);
//jsp页面代码,只要把下面代码放到页面里就能够了;
response.setContentType("application/unknown;charset=gbk");
response.addHeader("content-disposition","attachment;filename");
//jsp页面代码,只要把下面代码放到页面里就能够了;
//response.setContentType("application/unknown; charset=UTF-8");
//response.setHeader("Content-Disposition", "Attachment;filename= " + new String("导出.xls".getBytes("gb2312"), "ISO8859_1"));
%>