java、js的编码、解码

如果在地址栏挂载参数,特别是包含中文,往往要进行编码,取值时再解码,以下是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方法进行解码。

原文地址:https://www.cnblogs.com/wql025/p/4979634.html