1.urlencoder和urldecoder的使用

今天传url的时候乱码了。先说情形,url中有searchText=中文的情形,后台new String(searchText.getBytes(ISO-8859-1),"gbk")来获取,jsp中的是GBK的编码,服务器用的是 jboss,里面有个server.xml有如下配置。

<Connector port="80" address="${jboss.bind.address}"    
maxThreads="250" maxHttpHeaderSize="8192"
emptySessionPath="true" protocol="HTTP/1.1"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIENCODING="UTF-8"/>
之前是没有uriencoding这个属性的,我给干掉,问题解决,这时候用的是默认值即ISO-8859-1。

关于server.xml的配置可以参考这个url的文档

http://docs.jboss.org/jbossas/guides/webguide/r2/en/html/ch02.html

问题解决的过程中,我特意研究了一下urlencode和urldecode这两个类,之所以没有用这种方案是因为我获得页面上的连接的时候用的是一个开源的叫做Cloud的类。

网页中的表单使用POST方法提交时,数据内容的类型是 application/x-www-form-urlencoded,这种类型会: 1.字符"a"-"z","A"-"Z","0"-"9",".","-","*",和"_" 都不会被编码; 2.将空格转换为加号 (+) ; 3.将非文本内容转换成"%xy"的形式,xy是两位16进制的数值; 4.在每个 name=value 对之间放置 & 符号。
编码过程非常简单,任何字符只要不是ASCII码数字,字母,或者前面提到的标点符,它们都将被转换成字节形式,每个字节都写成这种形式:一个“%”后面 跟着两位16进制的数值。空格是一个特殊情况,因为它们太平常了。它除了被编码成“%20”以外,还能编码为一个“+”。加号(+)本身被编码为%2B。 当/ # = & 和?作为名字的一部分来使用时,而不是作为URL部分之间的分隔符来使用时,它们都应该被编码。
类URL并不自动执行编码或解码工作。你能生成一个URL对象,它可以包括非法的ASCII和非ASCII字符和/或%xx。当用方法getPath() 和toExternalForm( ) 作为输出方法时,这种字符和转移符不会自动编码或解码。你应对被用来生成一个URL对象的字符串对象负责,确保所有字符都会被恰当地编码。

URLencode这个类负责把String编码成平台上的通用形式,urldecode类可以把url转换成string格式。

下面是urlencode的demo:
public static void main(String[] args) {
        try {
         System.out.println(URLEncoder.encode("This string has spaces","UTF-8"));
         System.out.println(URLEncoder.encode("This*string*has*asterisks","UTF-8"));
         System.out.println(URLEncoder.encode("This%string%has%percent%signs", "UTF-8"));
         System.out.println(URLEncoder.encode("This+string+has+pluses","UTF-8"));
         System.out.println(URLEncoder.encode("This/string/has/slashes","UTF-8"));
         System.out.println(URLEncoder.encode("This"string"has"quote"marks", "UTF-8"));
         System.out.println(URLEncoder.encode("This:string:has:colons","UTF-8"));
         System.out.println(URLEncoder.encode("This~string~has~tildes","UTF-8"));
         System.out.println(URLEncoder.encode("This(string)has(parentheses)", "UTF-8"));
         System.out.println(URLEncoder.encode("This.string.has.periods","UTF-8"));
         System.out.println(URLEncoder.encode("This=string=has=equals=signs", "UTF-8"));
         System.out.println(URLEncoder.encode("This&string&has&ersands","UTF-8"));
         System.out.println(URLEncoder.encode("Thiséstringéhasé non-ASCII characters","UTF-8"));
         System.out.println(URLEncoder.encode("this中华人民共和国","UTF-8"));
       } catch (UnsupportedEncodingException ex) {throw new RuntimeException("Broken VM does not support UTF-8");
      }
     }
执行结果如下:
This+string+has+spaces
This*string*has*asterisks
This%25string%25has%25percent%25signs
This%2Bstring%2Bhas%2Bpluses
This%2Fstring%2Fhas%2Fslashes
This%22string%22has%22quote%22marks
This%3Astring%3Ahas%3Acolons
This%7Estring%7Ehas%7Etildes
This%28string%29has%28parentheses%29
This.string.has.periods
This%3Dstring%3Dhas%3Dequals%3Dsigns
This%26string%26has%26ersands
This%C3%A9string%C3%A9has%C3%A9+non-ASCII+characters
this%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD
很明显url中有/%&=这类字符也会被编码,对于我们来说是不对的。例如
public static void main(String[] args) {
        try {
            System.out.println(URLEncoder.encode("pg=q&kl=XX&stype=stext&q=+"Java+I/O"&search.x=38&search.y=3","UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
这个结果就是不对的了,会有如下输出:
pg%3Dq%26kl%3DXX%26stype%3Dstext%26q%3D%2B%22Java%2BI%2FO%22%26search.x%3D38%26search.y%3D3
所以这种情形我们要对每一部分做分段encode
pg=q&kl=XX&stype=stext&q=%2B%22Java+I%2FO%22&search.x=38&search.y=3
对于urldecoder类来说
它们解码以x-www-form-url-encoded这种形式编码的string。也就是说,它们把所有的加号(+)转换成空格符,把所有的%xx分别转换成与之相对应的字符

直接上demo:
public static void main(String[] args) {
        String input = "http://www.altavista.com/cgi-bin/" + "query?pg=q&kl=XX&stype=stext&q=%2B%22Java+I%2FO%22&search.x=38&search.y=3"; 
        String output;
        try {
            output = URLDecoder.decode(input, "UTF-8");
            System.out.println(output); 
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } 
    }
输入的结果如下:
http://www.altavista.com/cgi-bin/query?pg=q&kl=XX&stype=stext&q=+"Java I/O"&search.x=38&search.y=3

更多的可以参考这个文章:

http://www.java3z.com/cwbwebhome/article/article2/2414.html

总结一下,今天发生的中文乱码的问题最终的解决方案可能和urlencode和urldecode没有多大关系,这里有时间还要熟悉一下 jboss的server.xml的配置文件。

urlencode主要有encode方法,用来把url中非数字和字母的字符转换成%加两位16进制。

urldecode主要有decode方法,用来把一个含有%加两位16进制的url转换成正常的编码。

原文地址:https://www.cnblogs.com/sharpest/p/5541665.html