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

Java怎样举行Base64的编码(Encode)与解码(Decode)?

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

从以上程式可以发觉,在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”));

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

与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种别相对是首选!

© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发
火提提的头像-趣拿体育

昵称

取消
昵称表情代码图片