<pre name="code" class="html">import java.net.URLEncoder;
public class Test_1 {
/**
* @param args
*/
@SuppressWarnings("static-access")
public static void main(String[] args) {
try {
String msg = "中国";
System.out.println(msg.getBytes("utf-8"));//[B@45a27a8f
System.out.println(URLEncoder.encode(msg,"utf-8"));//%E4%B8%AD%E5%9B%BD
for(int i=0;i<msg.getBytes().length;i++){
System.out.print(Integer.toHexString(0xff & msg.getBytes("utf-8")[i]));//e4b8ade59bbd
}
/*是否证明两者是相同的,答案是否定的
因为URLEncode还会做一些特殊处理,对一些特殊字符串*/
String xys = " ";
System.out.println(URLEncoder.encode(xys ,"utf-8"));
// 打印出:"+"
System.out.println(Integer.toHexString(0xff & xys.getBytes("utf-8")[0]));
// 打印出:"20"
// 对于普通英文,两者也是一致的
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
getBates()和URLEncoder分析总结
最新推荐文章于 2022-03-27 15:27:58 发布