2011-05-26 92 views
229

在Java中,我想转换此:如何在Java中进行URL解码?

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

+0

@Stephen。为什么不能在URL中UTF-8编码的字符串..? – crackerplace 2011-05-26 12:14:21

+0

问题在于,仅仅因为URL可以是UTF-8,这个问题对于UTF-8来说真的没有用处。我已经适当地编辑了这个问题。 – 2011-05-26 12:19:06

+0

它可能是(理论上),但您的示例中的字符串不是UTF-8编码的字符串。它是一个URL编码的ASCII字符串。因此标题是误导性的。 – 2011-05-26 12:20:42

回答

483

这不会有任何与字符编码,如UTF-8或ASCII。你在那里的字符串是URL编码。这种编码与字符编码完全不同。

尝试这样:

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

。注意,字符编码(如UTF-8或ASCII)是什么决定字符的原始字节的映射。有关字符编码的介绍,请参见this article

+1

“URLDecoder”上的方法是静态的,因此您不必创建它的新实例。 – laz 2011-05-26 12:37:55

+0

@whataheck @whataheck使用URL编码是因为在某些地方,您不能在URL中使用各种字符,因此Stephen C在解释上述问题时会使用'%xx'代码转义某些字符。 – Jesper 2011-05-26 13:13:19

+0

您提供的方法标记为已弃用。为什么是这样的,什么是替代方案? – Trismegistos 2012-12-19 12:47:27

40

你得到的字符串是application/x-www-form-urlencoded编码。使用URLDecoder将其转换为Java字符串。

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

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

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

它不会转换%2C,它的(,) – vuhung3990 2015-01-06 18:45:56

+0

这需要包装在一个try/catch块..阅读更多关于检查异常(这一个)vs未经检查http://stackoverflow.com/questions/6115896/java -checked-vs-unchecked-exception-explanation – 2016-07-26 20:52:29

34

这已经回答了before(当然,这个问题是第一次!):

“你应该使用java.net.URI中要做到这一点,因为URLDecoder类做的X WWW的形式,进行了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 
+5

在Java 1.7中'URLDecoder.decode(String,String)'过载不被弃用。你必须引用'URLDecoder.decode(String)'过载而不用编码。您可能想要更新您的帖子以进行澄清。 – Aaron 2014-08-18 18:31:54

+1

这个答案有误导性,该块报价与弃用无关。不赞成使用的方法的Javadoc指出,我实际上引用了'@deprecated结果字符串可能因平台的默认编码而异。相反,使用decode(String,String)方法来指定编码。“ – 2015-04-01 10:30:19

+0

实际上只返回”/docs/english/site/mybook.do“对我来说 – Klever 2016-03-30 09:41:22

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

    } 

} 

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

4

我用apache commons

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

的默认字符集为UTF-8

+0

org.apache。 commons.codec.net现已弃用! – 2016-07-26 20:20:58

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

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

     } 

     return urlString; 

    } 
+0

您能详细解释一下您提供的解决方案吗? – abarisone 2015-06-16 07:22:45

相关问题