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

其他回答

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

“你应该使用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
public String decodeString(String URL)
    {

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

        }

        return urlString;

    }

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

URLDecoder.decode(URLDecoder.decode(url, StandardCharsets.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")); 

    } 

}

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

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

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