首页 > 其他 > 详细

使用response.setHeader("Content-Disposition","attachment;filename="+fName)下载文件,中文文件名无法显示的问题

时间:2014-02-18 15:47:54      阅读:782      评论:0      收藏:0      [点我收藏+]

今天遇到这么一个情况,在Action代码中进行文件下载:

  ActionForm得到file_id,通过file_id进行数据库查询得到file_name以及服务器硬盘上的file_uri,其中file_name是中文,然后通过如下代码下载

bubuko.com,布布扣
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename="+file_name);
OutputStream outputStream = response.getOutputStream();
InputStream inputStream = new FileInputStream(file_uri());
byte[] buffer = new byte[1024];
int i = -1;
while ((i = inputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, i);
}
outputStream.flush();
outputStream.close();
inputStream.close();
bubuko.com,布布扣

假设file_name的内容是:“工程文档.docx”

那么下载的结果就是一个名为“docx”的文件(文件名+后缀名),显然,文件名中的中文字符丢失了

上网查了资料,解决办法如下(我也感觉很不可思议,但的确管用了)

response.setContentType("application/x-download");
file_name = new String(file_name.getBytes(), "ISO-8859-1");
response.setHeader("Content-Disposition","attachment;filename="+file_name);
// 其他代码略

使用response.setHeader("Content-Disposition","attachment;filename="+fName)下载文件,中文文件名无法显示的问题

原文:http://www.cnblogs.com/qrlozte/p/3553744.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!