在Java中,我想将其转换为:
https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do%3Frequest_type
To this:
https://mywebsite/docs/english/site/mybook.do&request_type
这是我到目前为止所拥有的:
class StringUTF
{
public static void main(String[] args)
{
try{
String url =
"https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do" +
"%3Frequest_type%3D%26type%3Dprivate";
System.out.println(url+"Hello World!------->" +
new String(url.getBytes("UTF-8"),"ASCII"));
}
catch(Exception E){
}
}
}
但它不能正常工作。这些是什么%3A
and %2F
调用的格式以及如何转换它们?
这与 UTF-8 或 ASCII 等字符编码无关。你那里的字符串是URL编码。这种编码与字符编码完全不同。
尝试这样的事情:
try {
String result = java.net.URLDecoder.decode(url, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
// not going to happen - value came from JDK's own StandardCharsets
}
Java 10 添加了直接支持Charset
到 API,这意味着无需捕获 UnsupportedEncodingException:
String result = java.net.URLDecoder.decode(url, StandardCharsets.UTF_8);
请注意,一个字符编码(例如 UTF-8 或 ASCII)决定字符到原始字节的映射。有关字符编码的详细介绍,请参阅本文.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)