在MSIE中使用base64进行文件名编码

时间:2015-07-03 13:42:12

标签: java file encoding base64

我有一个方法:

  private String encodeFileAttachment(HttpServletRequest request, String filename) throws IOException {
String userAgent = request.getHeader("User-Agent");
if (userAgent.contains("Mozilla") && !userAgent.contains("MSIE")) {
    return "=?UTF-8?B?" + new String(Base64.encodeBase64(filename.getBytes("UTF-8")), "UTF-8") + "?=";
} else {
    return filename = URLEncoder.encode(filename, "UTF-8");
 }
}

和文件名如:

sss zzz ddd.png

firefox返回这样的内容: image1 但msie返回image 2

所以可能编码方法改变“”到'+' 该问题仅在Internet Explorer中出现。有人可以告诉我为什么吗?

1 个答案:

答案 0 :(得分:0)

这表现得如预期。 URLEncoder实现了HTML规范,用于编码HTML表单中的URL。

来自javadocs:

This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format.

并且来自HTML规范:

    1. Control names and values are escaped. Space characters are replaced by `+'

解决方案:

URLEncoder.encode(filename, "utf-8").replaceAll("\\+", " ");