当前位置:   article > 正文

Java 常用工具类(32) : base64解码与转码_java.util.base64

java.util.base64

参考 : string 字符串和字节 base64 json的相互转换_java string转base64_节点。csn的博客-CSDN博客 

  1. import java.util.Base64;
  2. /**
  3. * @Author: liyue
  4. * @Date: 2023/02/14/16:16
  5. * @Description:
  6. */
  7. public class Base64Util {
  8. public static void main(String[] args) {
  9. String str = "Hello World ! 你好世界!";
  10. String encode = encode(str);
  11. String decode = decode(encode);
  12. System.out.println("原字符串: [" + str + "]");
  13. System.out.println("转base64之后: [" + encode + "]");
  14. System.out.println("base64还原: [" + decode + "]");
  15. System.out.println("还原后与原字符串是否一致: [" + str.equals(decode) + "]");
  16. }
  17. // 字符串转base64
  18. public static String encode(String str) {
  19. byte[] bytes = str.getBytes();
  20. String base64Encode = Base64.getEncoder().encodeToString(bytes);
  21. return base64Encode;
  22. }
  23. // base64转字符串
  24. public static String decode(String base64Encode) {
  25. String decode64Str = new String(Base64.getDecoder().decode(base64Encode));
  26. return decode64Str;
  27. }
  28. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号