常用的js、java编码解码方法

  前言

  前后端直接传输数据进行交互不就行了吗,为什么还要进行编码解码?正常情况下直接交互没问题,但当有类似以下情况出现时就需要进行编码再进行传输:

    1、编码格式难以统一,导致数据交互过程出现中文乱码等问题;

    2、进行HTTP GET请求,参数是跟在URl上面,当参数的值有“/”、“&”等特殊字符时,将导致程序报错;

    3、进行HTTP POST请求,参数放在请求体里自由穿梭在前、后端,但人在江湖飘哪有不挨刀,程序员总是要经历一些奇奇怪怪的bug才能变强变秃,比如最近我们项目就碰到一个奇怪的bug,两边的编码格式已经一致,前端发送的是英文、半角状态下的密码字符串(字符串里有个美元符号),后端接到的数据中,半角状态的美元符号变成了全角状态的“$”,其他的字符正常,导致后续业务操作出现问题,而这两个状态下的没有符号变化不是很大,乍一看没看出来,导致这个bug我们排查了好久才解决...

 

  本文记录多种常用的js、java编码解码方法

 

  常用方法

  

  URL编码解码

  java

/* 
    javaURL编码解码,需要引入这两个JDK自带net包里面的类
    import java.net.URLDecoder;
    import java.net.URLEncoder;
*/
//URL编码
String encode = URLEncoder.encode("HuanZi!#123.qch@qq.com/fdfd", "UTF-8");
System.out.println(encode);
//HuanZi%21%23123.qch%40qq.com%2Ffdfd
//URL解码 String decode = URLDecoder.decode(encode, "UTF-8"); System.out.println(decode); //HuanZi!#123.qch@qq.com/fdfd

  js

/* 
    jsURL编码解码,我们使用encodeURIComponent、decodeURIComponent就可以了,它默认使用 UTF-8
*/
//URL编码
let encode = encodeURIComponent ("HuanZi!#123.qch@qq.com/fdfd");
console.log(encode);
//HuanZi!%23123.qch%40qq.com%2Ffdfd

//URL解码
let decode = decodeURIComponent(encode);
console.log(decode);
//HuanZi!#123.qch@qq.com/fdfd

 

  Base64编码解码

  java

  需要先maven引入apache提供的Base64工具类

<!-- Base64编码需要  -->
<dependency>
    <groupId>org.apache.directory.studio</groupId>
    <artifactId>org.apache.commons.codec</artifactId>
    <version>1.8</version>
</dependency>
View Code

 

/* 
    javaBase64编码解码,需要引入
    import org.apache.commons.codec.binary.Base64;
*/
//Base64编码
String encode1 = Base64.encodeBase64String("HuanZi!#123.qch@qq.com/fdfd".getBytes());
System.out.println(encode1);
//SHVhblppISMxMjMucWNoQHFxLmNvbS9mZGZk
            
//Base64解码
String decode1 = new String(Base64.decodeBase64(encode1));
System.out.println(decode1);
//HuanZi!#123.qch@qq.com/fdfd

  js

  先创建Base64工具对象(参考MDN的Base64编码和解码思路之一:https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Solution_4_%E2%80%93_escaping_the_string_before_encoding_it

let Base64 = {
    encode(str) {
        // first we use encodeURIComponent to get percent-encoded UTF-8,
        // then we convert the percent encodings into raw bytes which
        // can be fed into btoa.
        return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
            function toSolidBytes(match, p1) {
                return String.fromCharCode('0x' + p1);
            }));
    },
    decode(str) {
        // Going backwards: from bytestream, to percent-encoding, to original string.
        return decodeURIComponent(atob(str).split('').map(function (c) {
            return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
        }).join(''));
    }
};
View Code

 

/* 
    js Base64编码解码,需要先创建Base64工具对象
*/
//Base64编码
let encoded = Base64.encode("HuanZi!#123.qch@qq.com/fdfd");
console.log(encoded);
//SHVhblppISMxMjMucWNoQHFxLmNvbS9mZGZk

//Base64解码
let decoded = Base64.decode(encoded);
console.log(decoded);
//HuanZi!#123.qch@qq.com/fdfd

 

  后记

  一定要保证前后端编码、解码一致,否则会造成一端编码,另一端解码失败的严重后果

  更多方法持续更新中...

posted @ 2019-06-14 17:56  huanzi-qch  阅读(1701)  评论(0编辑  收藏  举报