如何在Java中进行URL解码?

时间:2011-05-26 12:00:20

标签: java url-encoding

在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格式被调用了什么以及如何转换它们?

11 个答案:

答案 0 :(得分:581)

这与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

答案 1 :(得分:46)

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

使用URLDecoder将其转换为Java String。

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

答案 2 :(得分:41)

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

  

“您应该使用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

答案 3 :(得分:14)

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

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

答案 4 :(得分:5)

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

答案 5 :(得分: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;

    }

答案 6 :(得分:3)

我使用apache commons

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

默认字符集为UTF-8

答案 7 :(得分:1)

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

    } 

}

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

答案 8 :(得分:0)

仅使用一次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();  
                }  
      }

答案 9 :(得分:0)

使用java.net.URI类:

public String getDecodedURL(String encodedUrl) {
    try {
        URI uri = new URI(encodedUrl);
        return uri.getScheme() + ":" + uri.getSchemeSpecificPart();
    } catch (Exception e) {
        return "";
    }
}

请注意,异常处理可能会更好,但是与该示例无关。

答案 10 :(得分:0)

如果它是整数值,我们也必须捕获 NumberFormatException。

try {
        Integer result = Integer.valueOf(URLDecoder.decode(urlNumber, "UTF-8"));
    } catch (NumberFormatException | UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }