ITPub博客

首页 > 应用开发 > > Servlet关于下载时中文名附件的一些问题?

Servlet关于下载时中文名附件的一些问题?

作者:flynike 时间:2019-07-26 18:28:43 0 删除 编辑

Servlet技术在下载中文名称附件的时候都需要对文件名进行重新编码

例如以下这种办法:

if (agent.contains("MSIE")) {        // IE浏览器
        filename = URLEncoder.encode(filename, "utf-8");
        filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {        // 火狐浏览器BASE64Encoder base64Encoder = new BASE64Encoder();
        filename = "=?utf-8?B?"
                + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
} else {        // 其它浏览器
        filename = URLEncoder.encode(filename, "utf-8");                
}
 
其中agent就是请求头User-Agent的值
 
一直这么用,但是有几个问题一直不明白
1.为什么要用URLEncoder.encode(filename, "utf-8");编码成application/x-www-form-urlencoded MIME 格式?我整个web应用环境都是UTF-8,为什么还要用UTF-8进行编码?2.application/x-www-form-urlencoded MIME 格式这个不是客户端向服务器请求的格式么?为什么服务器响应的时候也要用这种格式?3.浏览器到底能解析什么编码呢?我看到使用new String(fileName1.getBytes(“UTF-8”),”ISO8859-1”);这种方法在谷歌,火狐也可以进行中文文件名的解析。没有方法通知浏览器根据某种编码进行解码么?


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2618/viewspace-3155170/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-07-26

  • 博文量
    205
  • 访问量
    9825