假设我的网址中有空格,将其转换为 %20 的正确方法是什么?请不要提出“更换”建议。
\n\n例如,如果您将“ http://test.com/test and test/a”放入浏览器窗口中,它将转换为http://test.com/test%20and%20test/a
\n\n如果我使用 URLEncoder,我什至可以转换 / 。这不是我想要的。
\n\n谢谢,
\n\n这似乎是正确的方法。添加到问题,如果我想使用 utf8 编码转换为有效 url 的路径中还有一些非 ASCII 代码怎么办?例如\xef\xbc\x9a test.com:8080/test 和 test/pierlag2_carr\xc3\xa9/a?query=\xe4\xb8\x96\xe7\x95\x8c 我希望将其转换为 test .com:8080/test%20and%20test/pierlag2_carr%C3%A9/a?query=%E4%B8%96%E7%95%8C
\nURI尝试在班级的帮助下分成URL:
String sUrl = "http://test.com:8080/test and test/a?query=world";
URL url = new URL(sUrl);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
String canonical = uri.toString();
System.out.println(canonical);
Run Code Online (Sandbox Code Playgroud)
输出:
String sUrl = "http://test.com:8080/test and test/a?query=world";
URL url = new URL(sUrl);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
String canonical = uri.toString();
System.out.println(canonical);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4132 次 |
| 最近记录: |