今天传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的类。
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类来说
直接上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转换成正常的编码。