您的位置:首页 > 编程语言 > Java开发

java、js的编码、解码

2015-11-20 02:05 435 查看
如果在地址栏挂载参数,特别是包含中文,往往要进行编码,取值时再解码,以下是java和js中编码、解码的各自方法。

java:

@Test
public void test3() throws UnsupportedEncodingException{
System.out.println(URLEncoder.encode("我", "UTF-8"));//%E6%88%91
System.out.println(URLDecoder.decode("%E6%88%91", "UTF-8") );//我
}


可以看到先是用URLEncoder.encode对中文“我”进行编码,在用URLDecoder.decode方法进行解码。

js:

alert(encodeURIComponent('我'))//"%E6%88%91"
alert(decodeURIComponent(encodeURIComponent('我')) )//我


在js中,先用encodeURIComponent方法对中文“我”进行编码,在用encodeURIComponent方法进行解码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: