前言

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

    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>
/*
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(''));
}
};
/*
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

  后记

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

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

最新文章

  1. Centos6 服务器病毒查杀命令历史
  2. jvm垃圾回收机制
  3. System.Data.OleDb操作access数据库类,【bubuko.com】
  4. Verilog学习笔记设计和验证篇(二)...............同步有限状态机
  5. IDoc
  6. 【python】二进制、八进制、十六进制表示方法(3.0以上)
  7. MySQL执行外部sql脚本
  8. Accord.NET_Naive Bayes Classifier
  9. LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)
  10. The algorithm learning of sort which include Bubblesort,Insertsort,Quicksort and Mergesort.
  11. 遇到短信轰炸,别人换ip调你的短信接口怎么办
  12. virtualenv与virtualenvwrapper虚拟环境
  13. Spring Boot 2.0(一):Spring Boot 2.0尝鲜-动态 Banner
  14. Kafka 集群部署
  15. Eclipse “cannot be resolved to a type”
  16. 第三方开源插件zTree的使用
  17. 利用sys.dm_db_index_physical_stats查看索引碎片等数据
  18. BZOJ 3437 小P的牧场(斜率优化DP)
  19. linux基础(8)-颜色显示
  20. 1047 邮票面值设计 (DFS+DP)

热门文章

  1. [CodeForces - 1225D]Power Products 【数论】 【分解质因数】
  2. 用dotnet core搭建web服务器(三)ORM访问数据库
  3. win7安装centos7虚拟机
  4. IT兄弟连 HTML5教程 CSS3揭秘 CSS常见的样式属性和值1
  5. 基于Casbin实现ABAC
  6. css 揭秘-读书笔记
  7. Sql sever DateDiff 函数
  8. 利用css将英文转为大写或小写
  9. choose Perseverance :)
  10. 使用Settings sync同步VS Code配置