您的位置:首页 > 其它

Response.setHeader("Content-Disposition", "attachment; filename=" + fileName+".xls");file.Name为中文则乱码

2016-10-28 10:47 609 查看
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)下载也是乱码。

<%@ page import="java.io.*"%>

<%@ page import="java.util.*"%>

<%@ page contentType="text/html;charset=GBK"%>

<%

String key = request.getParameter("key");
String title = request.getParameter("file");
if(title == null) title="downLoad_table";

  title += ".xls";

  System.out.println("JILNJJHXJKHJHJHJHJ"+title);
if(key != null && !key.equals(""))
{
response.setContentType("application/x-msdownload");
response.setHeader("Content-type","application/x-msdownload");
response.setHeader("Accept-Ranges","bytes");
response.setHeader("Content-Disposition","attachment; filename="+ java.net.URLEncoder.encode(title, "UTF-8"));
String str = (String) session.getAttribute(key);
OutputStream os = response.getOutputStream();

    String header = "<html><head><title></title>\n" +

        "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=GBK\">\n" +

        "<meta name=ProgId content=Excel.Sheet>\n" +

        "<style>\n" +

        "<!--\n" +

        "a {\n" +

        "font-size:          9pt;\n" +

        "color:              navy;\n" +

        "text-decoration:    none;\n" +

        "}\n" +

        "a:hover {\n" +

        "font-size:          9pt;\n" +

        "color:              darkorange;\n" +

        "text-decoration:    underline;\n" +

        "}\n" +

        "-->\n" +

        "</style>\n" +

        "</head>\n" +

        "<body>\n";

    os.write(header.getBytes("GBK"));

    os.write(str.getBytes("GBK"));

    os.write("</body></html>".getBytes("GBK"));
os.flush();
os.close();
session.removeAttribute(key);
return;
}
else
{
session.removeAttribute(key);

%>

<html>

<!--

/*****************************************************************************

 * function:

 * Version 1.0

 * Author: leo Leo.Chou decay@163.com

 * create time : 2006.06.01

 * You can copy and/or use and/or modify this program free,but please reserve

 * the segment above. Please mail me if you have any question, Good day!

 *****************************************************************************

 */

-->

<head>

<meta http-equiv="Content-Type" content="text/html; charset=GBK">

<title></title></head>

<body bgcolor="#F0FAFF" text="#FF8040" marginwidth="0" marginheight="0" leftmargin="5" topmargin="1">

<script language="JavaScript">

<!--
alert("下载统计表错误!");

-->

</script>

</body></html>

<%
return;
}

%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐