Java如何进行Base64的编码(Encode)与解码(Decode)?

2018-03-20 22:01:18

关于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"));1234567891011121314151617181920

从以上程式可以发现,在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"));1234567891011121314151617

以上的程式码看起来又比早期用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"));12345678910111213141516171819

与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类别绝对是首选!

ps:


  • 2019-12-03 15:50:00

    html5 audio stop功能

    html5并没有提供停止功能,我们需要通过其他方式来实现这个问题,下面我们来看下神仙般的操作。

  • 2019-12-03 16:33:49

    hapi,nuxtjs跨域请求

    简单请求 与 预检请求,Fetch 与 CORS 的一个有趣的特性是,可以基于 HTTP cookies 和 HTTP 认证信息发送身份凭证。一般而言,对于跨域 XMLHttpRequest 或 Fetch 请求,浏览器不会发送身份凭证信息。如果要发送凭证信息,需要设置 XMLHttpRequest 的某个特殊标志位。

  • 2019-12-03 16:36:03

    跨域资源共享 CORS 详解

    阮一峰大哥的文章写的不错,推荐,也推荐他的整个王章,大家可以去看一下啊。

  • 2019-12-03 16:37:01

    去除options,减少options的访问

    因为跨域请求,浏览器可能(后面讲)会发送一次options请求,如果处理不好,跨域还是会gg的。 之前很少涉及跨域,涉及也是简单请求(下面阮老师文章中区别热简单请求和复杂请求),所以基本不会很少关注options。后面就遇到坑了,下面讲讲注意点。

  • 2019-12-04 10:46:26

    nuxt.js项目中全局捕获异常并生成错误日志全过程

     需求:客户在使用过程中页面报错时,可以生成错误记录传回服务器,以便改进。   步骤:     一.全局捕获异常,     二.发送到服务端,     三.生成错误日志。   一.全局捕获异常 如图,vue提供了errorHandle这个方法来处理全局异常,更多详细内容参见官网。

  • 2019-12-04 10:47:59

    nuxt.js项目中全局捕获异常并生成错误日志全过程

     需求:客户在使用过程中页面报错时,可以生成错误记录传回服务器,以便改进。   步骤:     一.全局捕获异常,     二.发送到服务端,     三.生成错误日志。   一.全局捕获异常 如图,vue提供了errorHandle这个方法来处理全局异常,更多详细内容参见官网。