如何在Java中进行URL解码? [英] How to do URL decoding in Java?

查看:121
本文介绍了如何在Java中进行URL解码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

But it doesn't work right. What are these %3A and %2F formats called and how do I convert them?

推荐答案

这与字符编码(如UTF-8或ASCII)无关。你在那里的字符串是 URL编码。这种编码与字符编码完全不同。

This does not have anything to do with character encodings such as UTF-8 or ASCII. The string you have there is URL encoded. This kind of encoding is something entirely different than character encoding.

尝试这样的事情:

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

请注意字符编码(例如UTF-8或ASCII)是什么决定了字符到原始字节的映射。有关字符编码的详细介绍,请参阅本文

Note that a character encoding (such as UTF-8 or ASCII) is what determines the mapping of characters to raw bytes. For a good intro to character encodings, see this article.

这篇关于如何在Java中进行URL解码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆