如何在下载文件时为不同的浏览器正确设置中文文件名

时间:2014-03-11 07:41:42

标签: java internet-explorer google-chrome firefox

我这样设置:

response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("你好.txt", "utf-8"));

它适用于chrome,但不适用于firefox,在firefox中文件名为%E4%BD%A0%E5%A5%BD.txt

那么如何设置文件名?我想在chrome,firefox和IE(> = 8)

中正确显示文件名

PS :由朋友解决,与您分享,也许可以帮助他人。

/**
  return encoded file name
*/
protected String getFileName(String filename) {
    try{
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("User-Agent").indexOf("MSIE") != -1) {
            return '\"' + java.net.URLEncoder.encode(filename, "UTF-8") + '\"';
        }
        byte[] bytes = filename.getBytes("UTF-8");
        StringBuilder buff = new StringBuilder(bytes.length << 2);
        buff.append("=?UTF-8?Q?");
        for (byte b : bytes) {
            int unsignedByte = b & 0xFF;
            buff.append('=').append(HEX_CHARS[unsignedByte >> 4]).append(HEX_CHARS[unsignedByte & 0xF]);
        }
        return buff.append("?=").toString();
    }catch(UnsupportedEncodingException e){
        return filename;
    }
}

private static final char[] HEX_CHARS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
        'C', 'D', 'E', 'F' };

我在chrom 33.0,IE 8和firefox 26.0中测试了这段代码,效果很好。

3 个答案:

答案 0 :(得分:2)

虽然这是旧帖,但我想分享我得到的解决方案。它可能会帮助某人。它就像IE 11,Chrome 48和Fire-Fox 38的魅力一样。以下是我在java代码中设置content-disposition的方法 -

final String encodedFileName = encodeFilename(fileNameToEncode); 
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName);

private String encodeFilename(final String filename)
{
  try{        
    URI uri = new URI(null, null, filename, null);      
    String encodedName = uri.toASCIIString(); 
    return encodedName;
  }
  catch(URISyntaxException ex){
      return filename;
  }
}

参考RFC以查看filename*=utf-8''

的使用情况

答案 1 :(得分:1)

这是我使用的最终解决方案。(使用Spring MVC)

 /**
  return encoded file name
   */
protected String getFileName(String filename) {
    try{
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("User-Agent").indexOf("MSIE") != -1) {
            return '\"' + java.net.URLEncoder.encode(filename, "UTF-8") + '\"';
        }
        byte[] bytes = filename.getBytes("UTF-8");
        StringBuilder buff = new StringBuilder(bytes.length << 2);
        buff.append("=?UTF-8?Q?");
        for (byte b : bytes) {
            int unsignedByte = b & 0xFF;
            buff.append('=').append(HEX_CHARS[unsignedByte >> 4]).append(HEX_CHARS[unsignedByte & 0xF]);
        }
        return buff.append("?=").toString();
    }catch(UnsupportedEncodingException e){
        return filename;
    }
}

private static final char[] HEX_CHARS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
        'C', 'D', 'E', 'F' };

答案 2 :(得分:0)

使用java.net.URI。像这样改变你的代码

 URI uri = new URI(null, null, "你好.txt", null);
 String fileName = uri.toASCIIString();
 response.setHeader("content-disposition", "attachment;filename=" + fileName);