求助:
 
我的tomcat是在OpenSolaris中,当使用ie下载中文文件时(我使用的ie7),总是不能下载。但在firefox中却能下载,但是文件名却显示不正确。
具体代码如下:
                  request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
String fileName = request.getParameter("fileName");
//fileName=getStr1(fileName);
fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
System.out.println(filPath + fileName);
File f = new File(filPath + fileName);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset();
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="
+ f.getName());
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();

解决方案 »

  1.   

    问题应该出在这里
    fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
    应该是gb2312吧?
    fileName = new String(fileName.getBytes("gb2312"), "UTF-8");
      

  2.   


    上面既然
    response.setCharacterEncoding("gb2312");后面就得这样:
    response.reset(); 
    response.setContentType("application/x-msdownload"); 
    // content-disposition 中的 filename 也应该以 gb2312 字符集进行 URI 编码
    response.setHeader("Content-Disposition", "attachment; filename=" 
    + java.net.URLEncoder.encode(f.getName(), "gb2312"));
      

  3.   

    谢谢各位,问题解决了。
    应该是这样的:
    response.reset(); 
    response.setContentType("application/x-msdownload");
    response.setHeader("Content-Disposition","attachment; filename="+java.net.URLEncoder.encode(f.getName(),"UTF-8"));