我在期待

System.out.println(java.net.URLEncoder.encode("Hello World", "UTF-8"));

输出:

你好%20世界

(20是ASCII十六进制空格码)

然而,我得到的是:

你好+世界

我用错方法了吗?我应该使用的正确方法是什么?


当前回答

它不是一行代码,但是你可以使用:

URL url = new URL("https://some-host.net/dav/files/selling_Rosetta Stone Case Study.png.aes");
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
System.out.println(uri.toString());

这将给你一个输出:

https://some-host.net/dav/files/selling_Rosetta%20Stone%20Case%20Study.png.aes

其他回答

该类执行application/x-www-form- urlenencoded -type编码,而不是百分比编码,因此替换为+是正确的行为。

从javadoc:

When encoding a String, the following rules apply: The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same. The special characters ".", "-", "*", and "_" remain the same. The space character " " is converted into a plus sign "+". All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default encoding of the platform is used.

我用错方法了吗?我应该使用的正确方法是什么?

是的,这个方法java.net.URLEncoder.encode并不是根据规范将“”转换为“20%”。

空格字符“”被转换为加号“+”。

即使这不是正确的方法,您也可以将其修改为:System.out.println(java.net.URLEncoder.encode(“Hello World”,“UTF-8”)。replaceAll("\\+", "%20"));祝你今天愉快=)。

URLEncoder使用字符集“ISO-8859-1”

查看uri类。

一个空格在url中被编码为%20,在表单提交的数据中被编码为+(内容类型为application/x-www-form-urlencoded)。你需要前者。

使用番石榴:

dependencies {
     compile 'com.google.guava:guava:23.0'
     // or, for Android:
     compile 'com.google.guava:guava:23.0-android'
}

你可以使用UrlEscapers:

String encodedString = UrlEscapers.urlFragmentEscaper().escape(inputString);

不要使用String。替换,这只会编码空间。使用库代替。