java urlencoding,url encoding - 如何在Java中进行URL解码?

url encoding - 如何在Java中进行URL解码?

在Java中,我想转换它:

https%3A%2F%2Fmywebsite%2Fdocs%2Fenglish%2Fsite%2Fmybook.do%3Frequest_type

对此:

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和%2F格式是什么叫,如何转换它们?

9个解决方案

542 votes

这与UTF-8或ASCII等字符编码无关。 你在那里的字符串是URL编码。 这种编码与字符编码完全不同。

尝试这样的事情:

String result = java.net.URLDecoder.decode(url, "UTF-8");

请注意,字符编码(例如UTF-8或ASCII)决定了字符到原始字节的映射。 有关字符编码的详细介绍,请参阅此文章。

Jesper answered 2019-02-09T13:48:08Z

45 votes

你得到的字符串是application/x-www-form-urlencoded编码。

使用URLDecoder将其转换为Java String。

URLDecoder.decode( url, "UTF-8" );

Alexander Pogrebnyak answered 2019-02-09T13:48:42Z

39 votes

之前已经回答过(尽管这个问题是第一个!):

“您应该使用java.net.URI来执行此操作,因为URLDecoder类执行x-www-form-urlencoded解码是错误的(尽管名称,它是表单数据)。”

基本上:

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

Nick Grealy answered 2019-02-09T13:49:23Z

13 votes

%3A和%2F是URL编码的字符。 使用此java代码将它们转换回:和/

String decoded = java.net.URLDecoder.decode(url, "UTF-8");

laz answered 2019-02-09T13:49:50Z

5 votes

try {

String result = URLDecoder.decode(urlString, "UTF-8");

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Hsm answered 2019-02-09T13:50:10Z

3 votes

我使用apache commons

String decodedUrl = new URLCodec().decode(url);

默认字符集是UTF-8

Sorter answered 2019-02-09T13:50:43Z

3 votes

public String decodeString(String URL)

{

String urlString="";

try {

urlString = URLDecoder.decode(URL,"UTF-8");

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

}

return urlString;

}

Ronak Poriya answered 2019-02-09T13:51:03Z

1 votes

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"));

}

}

你可以明智地选择你的方法:)

rinuthomaz answered 2019-02-09T13:51:32Z

0 votes

仅使用URLDecoder.decode一次是不够的。

例如 :

因为可以多次编码相同的URL,所以我们需要对其进行解码,直到URL无法进一步解码。 例如,“video%252Fmp4”是两个编码的结果。 解码一次后,我们得到“video%2Fmp4”。 现在需要进一步解码URL,以便得到“video / mp4”,这就是结果。

以下是适用于所有此类情况的代码:

public static String decode(String url)

{

try {

String prevURL="";

String decodeURL=url;

while(!prevURL.equals(decodeURL))

{

prevURL=decodeURL;

decodeURL=URLDecoder.decode( decodeURL, "UTF-8" );

}

return decodeURL;

} catch (UnsupportedEncodingException e) {

return "Issue while decoding" +e.getMessage();

}

}

Natesh bhat answered 2019-02-09T13:52:22Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值