4

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.

hiway
  • 3,906
  • 10
  • 34
  • 57

3 Answers3

5

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''

Community
  • 1
  • 1
Amit Gautam
  • 71
  • 1
  • 7
1

This is the final solution I use.(use 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' };
hiway
  • 3,906
  • 10
  • 34
  • 57
0

Use java.net.URI. Change your code like this

 URI uri = new URI(null, null, "你好.txt", null);
 String fileName = uri.toASCIIString();
 response.setHeader("content-disposition", "attachment;filename=" + fileName);
Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65