遇到java里的URLEncoder.encode方法编码后与javascript的encodeURIComponent方法的结果有点不一样,找了一下资料,原来URLEncoder实现的是HTML形式的规范,jdk文档里这么说:
Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format.
看到aliyun-oss-sdk的HttpUtil里是这么做的:
/*** Encode a URL segment with special chars replaced.*/public static String urlEncode(String value, String encoding) {if (value == null) {return "";}try {String encoded = URLEncoder.encode(value, encoding);return encoded.replace("+", "%20").replace("*", "%2A").replace("~", "%7E").replace("/", "%2F");} catch (UnsupportedEncodingException e) {throw new IllegalArgumentException(OSS_RESOURCE_MANAGER.getString("FailedToEncodeUri"), e);}}
参考:
URLEncoder not able to translate space character