赞
踩
参考 : string 字符串和字节 base64 json的相互转换_java string转base64_节点。csn的博客-CSDN博客
-
- import java.util.Base64;
-
- /**
- * @Author: liyue
- * @Date: 2023/02/14/16:16
- * @Description:
- */
- public class Base64Util {
-
- public static void main(String[] args) {
- String str = "Hello World ! 你好世界!";
- String encode = encode(str);
- String decode = decode(encode);
- System.out.println("原字符串: [" + str + "]");
- System.out.println("转base64之后: [" + encode + "]");
- System.out.println("base64还原: [" + decode + "]");
- System.out.println("还原后与原字符串是否一致: [" + str.equals(decode) + "]");
- }
-
- // 字符串转base64
- public static String encode(String str) {
- byte[] bytes = str.getBytes();
- String base64Encode = Base64.getEncoder().encodeToString(bytes);
- return base64Encode;
- }
-
- // base64转字符串
- public static String decode(String base64Encode) {
- String decode64Str = new String(Base64.getDecoder().decode(base64Encode));
- return decode64Str;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。