在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格式被称为什么?我如何转换它们?


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

使用URLDecoder将其转换为Java字符串。

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

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

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

这与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)决定字符到原始字节的映射。有关字符编码的介绍,请参阅本文。


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

“你应该使用java.net.URI来做这件事,因为URLDecoder类做的是x-www-form-urlencoded解码,这是错误的(尽管它的名字,它是为表单数据)。”

如URL类文档所述:

管理url编码和解码的建议方法是 使用URI,并使用toURI()和在这两个类之间进行转换 URI.toURL()。 URLEncoder和URLDecoder类也可以使用,但仅用于 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

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

    } 

}

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


我使用apache commons

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

默认字符集为UTF-8


 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;

    }

使用java.net.URI类:

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();
    }

我也有这个问题,来这里寻求答案。但是我用了那个问题被批准的朋友的代码,没用。我尝试了一些不同的方法,并且成功了,所以我分享了下面的代码行,以防它有所帮助。

URLDecoder.decode(URLDecoder.decode(url, StandardCharsets.UTF_8)))