import java.net.URLEncoder;
import java.net.URLDecoder;
public class Test
{
public static void main(String[] args)
{
try{
String s = "我";
byte[] bu = s.getBytes("UTF-8");
for(int i=0;i
String sutf8 = new String(bu,"UTF-8");
System.out.println("bu-utf8-"+ sutf8);
System.out.println("bu-gbk-"+ new String(bu,"GBK"));
// 将普通字符创转换成application/x-www-from-urlencoded字符串
String sutf8UrlEncode = URLEncoder.encode(sutf8, "UTF-8");
System.out.println(sutf8 +" UTF-8: URLEncoder==="+sutf8UrlEncode); //输出
// 将application/x-www-from-urlencoded字符串转换成普通字符串
String sutf8UrlDecode = URLDecoder.decode(sutf8UrlEncode, "UTF-8");
System.out.println(sutf8 +" UTF-8: URLDecoder==="+sutf8UrlDecode); //输出
System.out.println("\n\r==========================\n\r");
byte[] bg = s.getBytes("GBK");
for(int j=0;j
String sgkb = new String(bg,"GBK");
System.out.println("bg-utf8-"+ new String(bg,"UTF-8"));
System.out.println("bg-gbk-"+ sgkb);
// 将普通字符创转换成application/x-www-from-urlencoded字符串
String sgbkUrlEncode = URLEncoder.encode(sgkb, "GBK");
System.out.println(sgkb +" GBK: URLEncoder==="+sgbkUrlEncode); //输出
// 将application/x-www-from-urlencoded字符串转换成普通字符串
String sgbkUrlDecode = URLDecoder.decode(sgbkUrlEncode, "GBK");
System.out.println(sgkb +" GBK: URLDecoder==="+sgbkUrlDecode); //输出
}catch(Exception e){
}
}
/**
-1:1111 1111
-2:1111 1110
-3:1111 1101
-4:1111 1100
-5:1111 1011
-6:1111 1010
-7:1111 1001
-8:1111 1000
-9:1111 0111
-10:1111 0110
-11:1111 0101
-12:1111 0100
-13:1111 0011
-14:1111 0010
-15:1111 0001
-16:1111 0000
-17:1110 1111
-18:1110 1110
-19:1110 1101
-20:1110 1100
-21:1110 1011
-22:1110 1010
-23:1110 1001
-24:1110 1000
-25:1110 0111
-26:1110 0110 十六进制 0xE6 对应到urlencode后的值就是 %E6 (一字节8bit)
所谓的urlencode,不过是简单的将字节流,每个字节都用十六进制表示而已,也就是说:
举例:
汉字:我 用utf-8编码,对应的字节流数组bu是三个字节
bu:[0]==-26 -26 的十六进制表示 0xE6 ,用urlencode的表现形式是 %E6
bu:[1]==-120 -120 的十六进制表示 0x88 ,用urlencode的表现形式是 %88
bu:[2]==-111 -111 的十六进制表示 0x91 ,用urlencode的表现形式是 %91
0000:0
0001:1
0010:2
0011:3
0100:4
0101:5
0110:6
1110:E
1111:F
*/
/**
HTTP协议规定的URL传输规范:[0-9a-zA-Z] 和 $ - _ . + ! * ' ( ) , 这些字符能用在URL请求中
Only alphanumerics [0-9a-zA-Z], the special characters "$-_.+!*'()," [not including the quotes - ed],
and reserved characters used for their reserved purposes may be used unencoded within a URL
页面中的form表单提交,一般写法是: