下载文件时如何为不同的浏览器正确设置中文文件名 [英] how to set chinese filename correctly for different browsers when download file

查看:139
本文介绍了下载文件时如何为不同的浏览器正确设置中文文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

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



它在Chrome中工作,但不在Firefox中,文件名是%E4%BD%A0%E5%A5%BD.txt



那么如何设置文件名?我希望文件名在chrome,firefox和IE(> = 8)中正确显示。

PS

  / ** 
返回编码的文件名
* /
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中测试这个代码,效果很好。

解决方案

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

  final String encodedFileName = encodeFilename(fileNameToEncode); 
response.setHeader(Content-Disposition,attachment; filename * = UTF-8'+ encodedFileName);
$ b $ private String encodeFilename(final String filename)
{
try {
URI uri = new URI(null,null,filename,null);
字符串encodedName = uri.toASCIIString();
返回encodedName;

catch(URISyntaxException ex){
return filename;


引用 RFC 查看使用 filename * = utf-8'


I set like this:

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

it works in chrome, but not in firefox, in firefox the file name is %E4%BD%A0%E5%A5%BD.txt.

So how to set file name? I want the file name to be showed correctly in chrome, firefox and IE(>=8)

PS:solved by a friend, share it with you, maybe it can help others.

/**
  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' };

I test this code in chrom 33.0, IE 8 and firefox 26.0, it works well.

解决方案

Though this is old post, I'd like to share the solution I got. It may help someone. It worked like charm for IE 11, Chrome 48 and Fire-Fox 38. Here is how I set the content-disposition in my java code-

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;
  }
}

Ref the RFC to see the use of filename*=utf-8''

这篇关于下载文件时如何为不同的浏览器正确设置中文文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆