我有一个方法:
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返回这样的内容: 但msie返回
所以可能编码方法改变“”到'+' 该问题仅在Internet Explorer中出现。有人可以告诉我为什么吗?
答案 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("\\+", " ");