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


当前回答

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

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

其他回答

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

String decoded = java.net.URLDecoder.decode(url, "UTF-8");
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

使用java.net.URI类:

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

请注意,异常处理可以做得更好,但这与本例没有太大关系。

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

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

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