如何用Java进行URL解码?

问题描述 投票:314回答:8

在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格式分别是什么?如何转换它们?

java url-encoding
8个回答
619
投票

这与字符编码(例如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添加了对Charset的直接支持,这意味着无需捕获UnsupportedEncodingException:

String result = java.net.URLDecoder.decode(url, StandardCharsets.UTF_8);

请注意,字符编码(例如UTF-8或ASCII)决定了字符到原始字节的映射。有关字符编码的良好介绍,请参见this article


51
投票

您得到的字符串采用application/x-www-form-urlencoded编码。

使用URLDecoder将其转换为Java String。

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

46
投票

已被回答before(尽管这个问题是第一个!):

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

[URL类文档指出:

推荐的管理URL编码和解码的方法是使用URI,并使用toURI()URI.toURL()

URLEncoderURLDecoder类也可以使用,但仅适用于HTML表单编码,与编码方案不同在RFC2396中定义。

基本上:

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

14
投票

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

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

5
投票
 try {
        String result = URLDecoder.decode(urlString, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

5
投票
public String decodeString(String URL)
    {

    String urlString="";
    try {
        urlString = URLDecoder.decode(URL,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block

        }

        return urlString;

    }

3
投票

我使用apache commons

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

默认字符集为UTF-8


2
投票
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")); 

    } 

}

您可以明智地选择方法:)

© www.soinside.com 2019 - 2024. All rights reserved.