如何在Java中进行URL解码?
在Java中,我想转换以下内容: 为此: 这就是我到目前为止所做的:如何在Java中进行URL解码?,java,url-encoding,Java,Url Encoding,在Java中,我想转换以下内容: 为此: 这就是我到目前为止所做的: 但它不起作用。这些%3A和%2F格式的名称是什么?如何转换它们?您得到的字符串是application/x-www-form-urlencoded编码 用于将其转换为Java字符串 URLDecoder.decode( url, "UTF-8" ); %3A和%2F是URL编码的字符。使用此java代码将它们转换回:和/ 这与UTF-8或ASCII等字符编码无关。这里的字符串是URL编码的。这种编码与字符编码完全不同 试着这
但它不起作用。这些%3A和%2F格式的名称是什么?如何转换它们?您得到的字符串是application/x-www-form-urlencoded编码 用于将其转换为Java字符串
URLDecoder.decode( url, "UTF-8" );
%3A和%2F是URL编码的字符。使用此java代码将它们转换回:和/
这与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在API中添加了对字符集的直接支持,这意味着无需捕获UnsupportedEncodingException:
String result = java.net.URLDecoder.decode(url, StandardCharsets.UTF_8);
请注意,UTF-8或ASCII等字符编码决定了字符到原始字节的映射。有关字符编码的详细介绍,请参阅。尽管此问题是第一个,但此问题已得到解答!:
您应该使用java.net.URI来实现这一点,因为urldecker类执行x-www-form-urlencoded解码,这是错误的,尽管名称不同,但它用于表单数据
正如类文档所述:
管理URL编码和解码的推荐方法是
使用和在这两个类之间进行转换
和类也可以使用,但仅用于
HTML表单编码,与编码方案不同
定义于
基本上:
将为您提供:
https://mywebsite/docs/english/site/mybook.do?request_type
您可以明智地选择您的方法:我使用
使用java.net.URI类,默认字符集为UTF-8:
public String getDecodedURL(String encodedUrl) {
try {
URI uri = new URI(encodedUrl);
return uri.getScheme() + ":" + uri.getSchemeSpecificPart();
} catch (Exception e) {
return "";
}
}
请注意,异常处理可能会更好,但与本例无关。如果是整数值,我们还必须捕获NumberFormatException
try {
Integer result = Integer.valueOf(URLDecoder.decode(urlNumber, "UTF-8"));
} catch (NumberFormatException | UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@斯蒂芬。。为什么url不能是UTF-8编码的字符串?问题是,仅仅因为URL可以是UTF-8,这个问题实际上与UTF-8无关。我已经对这个问题进行了适当的编辑。理论上可能是这样,但您示例中的字符串不是UTF-8编码的字符串。它是一个URL编码的ASCII字符串。因此,标题具有误导性。还值得注意的是,url字符串中的所有字符都是ASCII字符,在对字符串进行url解码后也是如此。“%”是ASCII字符,如果xx小于十六进制80,则%xx表示ASCII字符。URLDecoder上的方法是静态的,因此您不必创建它的新实例。@Trismegisto只是不指定编码第二个参数的字符的版本,根据Java 7 API文档,UTF-8不推荐使用。使用带有两个参数的版本。如果使用java 1.7+,则可以使用UTF-8字符串的静态版本:StandardCharsets.UTF_8.name(来自此包):java.nio.charset.StandardCharsets。与此相关:对于字符编码,这也是一篇很好的文章,balusc.blogspot.in/2009/05/unicode-how-to-get-characters-right.htmlBe请注意这一点。正如这里提到的:这不是关于URL,而是关于HTML表单编码。在Java 1.7中,URLDecover.decodeString,字符串重载不是不推荐的。您必须引用没有编码的urldecker.decodeString重载。你可能想更新你的帖子来澄清问题。这个答案有误导性;那个块引号和反对意见无关。不推荐使用的方法的Javadoc声明,我实际上引用了@deprecated。根据平台的默认编码,结果字符串可能会有所不同。相反,请使用decodeString、String方法指定encoding。如上所述,URI的getPath仅返回URI的路径部分。除非我弄错了,否则路径是权限部分之后的URI部分。请参阅:有关路径的定义,在我看来,我看到的行为是标准/正确的行为。我在Android Studio上使用java 1.8.0_101。我很想知道你得到了什么,因为getAuthority被称为。即使是这篇文章/例子似乎也表明path只是他们URI中的/public/manual/appliances部分:@Pelpotronic文章中的代码实际上打印了它至少为我显示的输出。我认为这样做的原因是,由于URL编码,URI构造函数实际上将整个字符串https%3A%2F…,视为URI的路径;没有权限或查询等。可以通过在URI对象上调用相应的get方法来测试这一点。如果将解码文本传递给URI构造函数:newURIhttps://mywebsite/do.....,然后调用getPath和其他方法将给出正确的结果。它也不会转换%2C,它是,这需要包装在try/catch块中。。阅读更多关于已检查异常的信息此项与未检查异常的信息请详细说明您的答案并添加更多关于您提供的解决方案的描述?
String result = java.net.URLDecoder.decode(url, StandardCharsets.UTF_8);
String url = "https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do%3Frequest_type";
System.out.println(new java.net.URI(url).getPath());
https://mywebsite/docs/english/site/mybook.do?request_type
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
public class URLDecoding {
String decoded = "";
public String decodeMethod(String url) throws UnsupportedEncodingException
{
decoded = java.net.URLDecoder.decode(url, "UTF-8");
return decoded;
//"You should use java.net.URI to do this, as the URLDecoder class does x-www-form-urlencoded decoding which is wrong (despite the name, it's for form data)."
}
public String getPathMethod(String url) throws URISyntaxException
{
decoded = new java.net.URI(url).getPath();
return decoded;
}
public static void main(String[] args) throws UnsupportedEncodingException, URISyntaxException
{
System.out.println(" Here is your Decoded url with decode method : "+ new URLDecoding().decodeMethod("https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do%3Frequest_type"));
System.out.println("Here is your Decoded url with getPath method : "+ new URLDecoding().getPathMethod("https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do%3Frequest"));
}
}
String decodedUrl = new URLCodec().decode(url);
try {
String result = URLDecoder.decode(urlString, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public String decodeString(String URL)
{
String urlString="";
try {
urlString = URLDecoder.decode(URL,"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
}
return urlString;
}
public String getDecodedURL(String encodedUrl) {
try {
URI uri = new URI(encodedUrl);
return uri.getScheme() + ":" + uri.getSchemeSpecificPart();
} catch (Exception e) {
return "";
}
}
try {
Integer result = Integer.valueOf(URLDecoder.decode(urlNumber, "UTF-8"));
} catch (NumberFormatException | UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}