博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java Base64 编码解码方案总结
阅读量:6936 次
发布时间:2019-06-27

本文共 2672 字,大约阅读时间需要 8 分钟。

Base64是一种能将任意Binary资料用64种字元组合成字串的方法,而这个Binary资料和字串资料彼此之间是可以互相转换的,十分方便。在实际应用上,Base64除了能将Binary资料可视化之外,也常用来表示字串加密过后的内容。

早期作法

早期在Java上做Base64的编码与解码,会使用到JDK里sun.misc套件下的BASE64Encoder和BASE64Decoder这两个类别,用法如下:

final BASE64Encoder encoder = new BASE64Encoder();final BASE64Decoder decoder = newBASE64Decoder();final String text ="Java深入";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 ="Java深入";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 = "Java深入";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 = "Java深入";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 = "Java深入";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"));finalBase64.Decoder decoder = Base64.getDecoder();final Base64.Encoder encoder = Base64.getEncoder();final String text = "Java深入";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倍。

转载地址:http://viwnl.baihongyu.com/

你可能感兴趣的文章
java中对JVM的深度解析、调优工具、垃圾回收
查看>>
浅谈Java语言中ArrayList和HashSet的区别
查看>>
React文档(十四)深入JSX
查看>>
Spring集成线程池
查看>>
winform代码:关联窗体数据更新,删除dataGridview中选中的一行或多行
查看>>
Python基础 2
查看>>
java 内存溢出-与gc
查看>>
python django开发问题
查看>>
51nod1238 最小公倍数之和 V3
查看>>
扩展欧几里得 POJ 1061
查看>>
iOS开发之UIDevice通知
查看>>
与其倒推以前不如推到重建
查看>>
Could not find Developer Disk Image
查看>>
表空间和文件系统使用率监控脚本
查看>>
Unix高级环境编程—进程控制(一)
查看>>
面向对象进阶
查看>>
Python 携程
查看>>
相约深圳~敏捷之旅
查看>>
报错OPTION SQL_SELECT_LIMIT=
查看>>
js单例模式
查看>>