详解Java如何进行Base64的编码(Encode)与解码(Decode)
作者:南望孤笑 时间:2023-01-31 18:53:34
关于base64编码Encode和Decode编码的几种方式
Base64是一种能将任意Binary资料用64种字元组合成字串的方法,而这个Binary资料和字串资料彼此之间是可以互相转换的,十分方便。在实际应用上,Base64除了能将Binary资料可视化之外,也常用来表示字串加密过后的内容。如果要使用Java 程式语言来实作Base64的编码与解码功能,可以参考本篇文章的作法。
早期作法
早期在Java上做Base64的编码与解码,会使用到JDK里sun.misc套件下的BASE64Encoder和BASE64Decoder这两个类别,用法如下:
final BASE64Encoder encoder = new BASE64Encoder();
final BASE64Decoder decoder = new BASE64Decoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//编码
final String encodedText = encoder.encode(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(decoder.decodeBuffer(encodedText), "UTF-8"));
final BASE64Encoder encoder = new BASE64Encoder();
final BASE64Decoder decoder = new BASE64Decoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//编码
final String encodedText = encoder.encode(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(decoder.decodeBuffer(encodedText), "UTF-8"));
从以上程式可以发现,在Java用Base64一点都不难,不用几行程式码就解决了!只是这个sun.mis c套件所提供的Base64功能,编码和解码的效率并不太好,而且在以后的Java版本可能就不被支援了,完全不建议使用。
Apache Commons Codec作法
Apache Commons Codec有提供Base64的编码与解码功能,会使用到org.apache.commons.codec.binary套件下的Base64类别,用法如下:
final Base64 base64 = new Base64();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//编码
final String encodedText = base64.encodeToString(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(base64.decode(encodedText), "UTF-8"));
final Base64 base64 = new Base64();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//编码
final String encodedText = base64.encodeToString(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(base64.decode(encodedText), "UTF-8"));
以上的程式码看起来又比早期用sun.mis c套件还要更精简,效能实际执行起来也快了不少。缺点是需要引用Apache Commons Codec,很麻烦。
Java 8之后的作法
Java 8的java.util套件中,新增了Base64的类别,可以用来处理Base64的编码与解码,用法如下:
final Base64.Decoder decoder = Base64.getDecoder();
final Base64.Encoder encoder = Base64.getEncoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//编码
final String encodedText = encoder.encodeToString(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
final Base64.Decoder decoder = Base64.getDecoder();
final Base64.Encoder encoder = Base64.getEncoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//编码
final String encodedText = encoder.encodeToString(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
与sun.mis c套件和Apache Commons Codec所提供的Base64编解码器来比较的话,Java 8提供的Base64拥有更好的效能。实际测试编码与解码速度的话,Java 8提供的Base64,要比sun.mis c套件提供的还要快至少11倍,比Apache Commons Codec提供的还要快至少3倍。因此在Java上若要使用Base64,这个Java 8底下的java .util套件所提供的Base64类别绝对是首选!
来源:https://www.cnblogs.com/alter888/p/9140732.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现发送邮件的示例代码
![](https://img.aspxhome.com/file/2023/6/68216_0s.png)
SSM框架实现分页和搜索分页的示例代码
C语言字符串操作总结大全(超详细)
详解C#中的定时器Timer类及其垃圾回收机制
C#之CLR内存深入分析
![](https://img.aspxhome.com/file/2023/4/108764_0s.png)
Android 自定义标题栏 显示网页加载进度的方法实例
C#浏览器提示跨域问题解决方案
![](https://img.aspxhome.com/file/2023/0/108930_0s.png)
c# WPF实现Windows资源管理器(附源码)
![](https://img.aspxhome.com/file/2023/3/83313_0s.png)
Java Config下的Spring Test几种方式实例详解
设计模式系列之组合模式及其在JDK和MyBatis源码中的运用详解
![](https://img.aspxhome.com/file/2023/6/68156_0s.png)
java实现纸牌游戏之小猫钓鱼算法
Java实现删除排序数组中重复元素的方法小结【三种方法比较】
Mybatis order by 动态传参出现的问题及解决方法
![](https://img.aspxhome.com/file/2023/3/64373_0s.png)
当Mybatis遇上目录树超全完美解决方案
![](https://img.aspxhome.com/file/2023/8/75778_0s.png)
SpringBoot Actuator潜在的OOM问题的解决
![](https://img.aspxhome.com/file/2023/5/119175_0s.png)
详解Java分布式系统中一致性哈希算法
![](https://img.aspxhome.com/file/2023/2/79902_0s.jpg)
Spring学习笔记之bean的基础知识
![](https://img.aspxhome.com/file/2023/3/63433_0s.png)
SpringRetry重试框架的具体使用
![](https://img.aspxhome.com/file/2023/5/82305_0s.png)