Java 6 中的 URL 解码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6519746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
URL Decode in Java 6
提问by danny.lesnik
I see that java.net.URLDecoder.decode(String)
is deprecated in 6.
我看到它java.net.URLDecoder.decode(String)
在 6 中已被弃用。
I have the following String:
我有以下字符串:
String url ="http://172.20.4.60/jsfweb/cat/%D7%9C%D7%97%D7%9E%D7%99%D7%9D_%D7%A8%D7%92%D7%99%D7%9C%D7%99%D7%9"
How should I decode it in Java 6?
我应该如何在 Java 6 中解码它?
采纳答案by Tim Cooper
Now you need to specify the character encoding of your string. Based off the information on the URLDecoder
page:
现在您需要指定字符串的字符编码。根据URLDecoder
页面上的信息:
Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilites.
The following should work for you:
java.net.URLDecoder.decode(url, "UTF-8");
注意:万维网联盟建议指出应使用 UTF-8。不这样做可能会导致不兼容。
以下应该对您有用:
java.net.URLDecoder.decode(url, "UTF-8");
Please see Draemon's answerbelow.
请参阅下面的Draemon 的回答。
回答by Joachim Sauer
As the documentationmentions, decode(String)
is deprecated because it always uses the platform default encoding, which is often wrong.
正如文档中提到的,decode(String)
不推荐使用,因为它总是使用平台默认编码,这通常是错误的。
Use the two-argument version instead. You will need to specify the encoding used n the escaped parts.
改用两个参数的版本。您需要指定在转义部分中使用的编码。
回答by Mathias Schwarz
Only the decode(String)
method is deprecated. You should use the decode(String, String)
method to explicitly set a character encoding for decoding.
仅decode(String)
弃用该方法。您应该使用该decode(String, String)
方法显式设置用于解码的字符编码。
回答by Draemon
You should use java.net.URIto do this, as the URLDecoder class does x-www-form-urlencoded decoding which is wrong (despite the name, it's for form data).
您应该使用java.net.URI来执行此操作,因为 URLDecoder 类执行 x-www-form-urlencoded 解码是错误的(尽管名称如此,但它用于表单数据)。
回答by DKroot
As noted by previous posters, you should use java.net.URI class to do it:
正如之前的海报所指出的,您应该使用 java.net.URI 类来做到这一点:
System.out.println(String.format("Decoded URI: '%s'", new URI(url).getPath()));
What I want to note additionally is that if you have a path fragment of a URI and want to decode it separately, the same approach with one-argument constructor works, but if you try to use four-argument constructor it does not:
我还要注意的是,如果您有一个 URI 的路径片段并想单独对其进行解码,则使用单参数构造函数的相同方法可以工作,但是如果您尝试使用四参数构造函数,则不会:
String fileName = "Map%20of%20All%20projects.pdf";
URI uri = new URI(null, null, fileName, null);
System.out.println(String.format("Not decoded URI *WTF?!?*: '%s'", uri.getPath()));
This was tested in Oracle JDK 7. The fact that this does not work is counter-intuitive, runs contrary to JavaDocs and it should be probably considered a bug.
这在 Oracle JDK 7 中进行了测试。事实上,这不起作用是违反直觉的,与 JavaDocs 相悖,它可能应该被视为一个错误。
It could trip people who are trying to use an approach symmetrical to encoding. As noted for example in this post: "how to encode URL to avoid special characters in java", in order to encodeURI, it's a good idea to construct a URI by passing different URI parts separately since different encoding rules apply to different parts:
它可能会绊倒那些试图使用与编码对称的方法的人。正如这篇文章中提到的例子:“如何编码 URL 以避免 java 中的特殊字符”,为了对URI进行编码,最好通过分别传递不同的 URI 部分来构造 URI,因为不同的编码规则适用于不同的部分:
String fileName2 = "Map of All projects.pdf";
URI uri2 = new URI(null, null, fileName2, null);
System.out.println(String.format("Encoded URI: '%s'", uri2.toASCIIString()));