文件下载:"Content-disposition","attachment; filename=中文名>>>解决方案
文件下载时常会出现如下问题: response.setHeader("Content-disposition","attachment; filename="+as+"");
如果as直接写中文名字时,就会直接在IE页面打开Excel文件(弹出后点打开会在IE页面打开,注意此时的“你想打开或保存此文件吗?”,名称会显示一个类似CAK....的名称,实现上并不是正常的 *.xls格式的名称),如图所示:
而不是另外弹出并打开Excel文件。解决办法是:(程序片段)
String as="我明白了.xls";
String fileName =as;// = java.net.URLEncoder.encode(as, "UTF-8");
/*根据request的locale 得出可能的编码,中文操作系统通常是gb2312*/
fileName = new String(as.getBytes("GB2312"), "ISO_8859_1");
as=fileName;
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition","attachment; filename="+as+"");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("" + filename)));
bis = new BufferedInputStream(new FileInputStream(dirFile));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
} catch(final IOException e) {
System.out.println ( "出现IOException." + e );
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
程序段中最关键的一句:
new String(as.getBytes("GB2312"), "ISO_8859_1");
将文件命名转码. OK结果正常了>>>

这样就可以下载时用中文名称了,并且是另外打开Excel文件的。
转载:http://cutelion.blog.hexun.com/6980666_d.html
文件下载:"Content-disposition","attachment; filename=中文名>>>解决方案的更多相关文章
- response.setHeader("Content-disposition","attachment;filename="+fileName) 下载时文件名中存在空格错误
最近在进行文件下载时发现一个问题,就是下面语句运行时,下载某些文件正常,下载某些文件异常,后来发现文件名中有空格的文件火狐浏览器是默认将文件名截断了的 response.setHeader(" ...
- 使用response.setHeader("Content-Disposition","attachment;filename="+fName)下载文件,中文文件名无法显示的问题
今天遇到这么一个情况,在Action代码中进行文件下载: ActionForm得到file_id,通过file_id进行数据库查询得到file_name以及服务器硬盘上的file_uri,其中file ...
- 【转】解决response.AddHeader("Content-Disposition", "attachment; fileName=" + fileName) 中文显示乱码
如果fileName为中文则乱码.解决办法是 方法1: response.setHeader("Content-Disposition", "attachment; fi ...
- firfox中"content-disposition", "attachment;filename=“+filename不能显示文件名字
一般要在浏览器中显示文件名,可以使用以下文件名 // 设置文件名的编码方式,使得文件的名字能够正常安全的显示. filename = URLEncoder.encode(filename, " ...
- 解决Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name) 中文显示乱码
如果file.Name为中文则乱码.解决办法是方法1:response.setHeader("Content-Disposition", "attachment; fil ...
- C#中解决Response.AddHeader("Content-Disposition", "attachment; filename=" + filename)下载文件时文件名乱码的问题
问题:下载文件时文件名乱码怎么解决? 在C#写后台代码过程中,经常遇到下载文件出现文件名乱码的问题,在网上找了很多方法,总是存在浏览器不兼容的问题,当IE浏览器不乱码时,火狐浏览器就会乱码,后来经过反 ...
- Response attachment filename 中文乱码
Response.setHeader("Content-Disposition", "attachment; filename=" + fileName+&qu ...
- content-disposition attachment filename 在Firefox和IE中得到不同的结果
在Firefox中需要把filename 用双引号包起来,才能得到想要的名字,不然如果含有空格,会丢掉空格后面的部分.而IE会把空格转为_,因此也需要HttpUtility.UrlPathEncode ...
- Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name) 中文显示乱码
如果file.Name为中文则乱码.解决办法是方法1:response.setHeader("Content-Disposition", "attachment; fil ...
随机推荐
- 深入浅出 消息队列 ActiveMQ(转)
一. 概述与介绍 ActiveMQ 是Apache出品,最流行的.功能强大的即时通讯和集成模式的开源服务器.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provide ...
- boost uuid 学习笔记
#include <vector>#include <iostream>#include <boost/uuid/uuid.hpp>#include <boo ...
- Solr4.2迁移到新项目下异常:java.lang.NoSuchMethodError: org.apache.http.conn.scheme.Scheme.<init>
由于业务调整,需要将solr搜索项目集成到另一个项目下成为一个模块,原项目运行异常,但是迁移到新项目后出现异常如下: 原因:引入的httpclient.jar冲突 解决方法:删除冲突的jar
- Maven项目红色叹号+JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java B
昨天写的关于解决JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the ...
- SpringMVC返回json数据的三种方式
1.第一种方式是spring2时代的产物,也就是每个json视图controller配置一个Jsoniew. 如:<bean id="defaultJsonView" cla ...
- Fibinary Numbers
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30506#problem/V 题意:从右向左,每一个位数,分别表示一个fibonacci数 ...
- AngelHack China 2013 招组队成员
AngelHack China 2013 connect me
- Codeforces Round #311 (Div. 2)
我仅仅想说还好我没有放弃,还好我坚持下来了. 最终变成蓝名了,或许这对非常多人来说并不算什么.可是对于一个打了这么多场才好不easy加分的人来说,我真的有点激动. 心脏的难受或许有点是由于晚上做题时太 ...
- JavaScript快速入门(三)——JavaScript语句
JavaScript基本语句 基本概述 JavaScript是脚本语言,从上到下解释执行,最小单位为语句或语句块,每个语句以分号结尾,每个语句块以右大括号结尾. JavaScript可以将多条语句或语 ...
- An example usage of an encryption algorithm (AES, in this case) is:
pycrypto 2.6.1 : Python Package Index An example usage of an encryption algorithm (AES, in this case ...