文件下载(IE)上的文件名损坏

时间:2008-09-30 14:26:46

标签: internet-explorer http internationalization http-headers

我已经实现了一个简单的文件上传下载机制。当用户单击文件名时,将使用以下HTTP标头下载该文件:

HTTP/1.1 200 OK
Date: Tue, 30 Sep 2008 14:00:39 GMT
Server: Microsoft-IIS/6.0
Content-Disposition: attachment; filename=filename.doc;
Content-Type: application/octet-stream
Content-Length: 10754

我也支持日文文件名。为此,我用这个java方法编码文件名:

private String encodeFileName(String name) throws Exception{
    String agent = request.getHeader("USER-AGENT");
    if(agent != null && agent.indexOf("MSIE") != -1){ // is IE
        StringBuffer res = new StringBuffer();
        char[] chArr = name.toCharArray();
        for(int j = 0; j < chArr.length; j++){
            if(chArr[j] < 128){ // plain ASCII char
                if (chArr[j] == '.' && j != name.lastIndexOf("."))
                    res.append("%2E");
                else
                    res.append(chArr[j]);
            }
            else{ // non-ASCII char
                byte[] byteArr = name.substring(j, j + 1).getBytes("UTF8");
                for(int i = 0; i < byteArr.length; i++){
                    // byte must be converted to unsigned int
                    res.append("%").append(Integer.toHexString((byteArr[i]) & 0xFF));
                }
            }
        }
        return res.toString();
    }
    // Firefox/Mozilla
    return MimeUtility.encodeText(name, "UTF8", "B");
}

到目前为止,它运作良好,直到有人发现它对长文件名不起作用。例如:あああああああああああああああ2008.10.1あ.doc。如果我将其中一个单字节点更改为单字节下划线,或者如果我删除第一个字符,则它可以正常工作。即,它取决于点字符的长度和URL编码。 以下是一些例子。

这已被破坏(あああああああああああああああ2008.10.1あ.doc):

Content-Disposition: attachment; filename=%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%822008%2E10%2E1%e3%81%82.doc;

这没关系(あああああああああああああああ2008_10.1あ.doc):

Content-Disposition: attachment; filename=%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%822008_10%2E1%e3%81%82.doc;

这也没关系(あああああああああああああああ2008.10.1あ.doc):

Content-Disposition: attachment; filename=%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%82%e3%81%822008%2E10%2E1%e3%81%82.doc;

有人有线索吗?

4 个答案:

答案 0 :(得分:6)

gmail处理文件名转义的方式略有不同:引用文件名(双引号),单字节句点不进行URL转义。 这样,问题中的长文件名就可以了。

Content-Disposition: attachment; filename="%E3%81%82%E3%81%82%E3%81%82%E3%81%82%E3%81%82%E3%81%82%E3%81%82%E3%81%82%E3%81%82%E3%81%82%E3%81%82%E3%81%82%E3%81%82%E3%81%82%E3%81%822008.10.1%E3%81%82.doc"

但是,文件名的字节长度仍然存在限制(显然只有IE)(我假设是一个错误)。因此,即使文件名仅由单字节字符组成,文件名的开头也会被截断。 限制大约是160个字节。

答案 1 :(得分:2)

如上所述,如果没有浏览器嗅探并为每个浏览器返回不同的标题,Content-Disposition和Unicode就无法在所有主浏览器中运行。

我的解决方案是完全避免使用Content-Disposition标头,并将文件名附加到URL的末尾,以欺骗浏览器认为它直接获取文件。 e.g。

http://www.xyz.com/cgi-bin/dynamic.php/あああああああああああああああ2008.10.1あ.doc

这自然假设您在创建链接时知道文件名,尽管快速重定向标头可以根据需要设置它。

答案 2 :(得分:1)

这里的主要问题是IE不支持相关的RFC,这里是:RFC2231。见pointers and test cases。此外,您用于IE的解决方法(仅使用转义百分比的UTF-8)还有其他几个问题;它可能无法在所有语言环境中工作(据我记得,该方法在韩国失败,除非IE配置为始终在URL中使用UTF-8,这不是默认值),并且如前所述,有长度限制(I听说 在IE8中修复了,但我还没试过。)

答案 3 :(得分:-2)

我认为这个问题在IE8中得到修复,我看到它在IE 8中工作。

相关问题