Java如何进行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"));

只是这个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"));

以上的程式码看起来又比早期用sun.mis c套件要精简,效能实际执行起来也快了不少。缺点是需要引用Apache Commons Codec,导包:commons-codec-1.11.jar

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"));

这个Java 8底下的java .util套件所提供的Base64类别绝对是首选!
来源:https://magiclen.org/java-base64/

猜你喜欢

转载自blog.csdn.net/tyt_xiaotao/article/details/79182459
今日推荐