2020-06-29【Java:Base64编码解码】

2020-06-29  本文已影响0人  skillplus

Base64编码解码已经加入Java 8 类库的标准。

参考文档地址:https://www.runoob.com/java/java8-base64.html

public class Test {
  public static void main(String[] args) throws Exception {
    String str = "锄禾日当午,汗滴禾下土。\n谁知盘中餐,粒粒皆辛苦。";
    
    String base64_1 = Base64.getEncoder().encodeToString(str.getBytes("utf-8"));
    System.out.println(base64_1);
    System.out.println("*******");
    
    String base64_2 = Base64.getMimeEncoder().encodeToString(str.getBytes("utf-8"));
    System.out.println(base64_2);
    System.out.println("*******");

    Decoder decoder = Base64.getMimeDecoder();
    
    byte[] bs_1 = decoder.decode(base64_1);
    System.out.println(new String(bs_1, "utf-8"));
    System.out.println("*******");
    
    byte[] bs_2 = decoder.decode(base64_2);
    System.out.println(new String(bs_2, "utf-8"));
  }
}
上一篇下一篇

猜你喜欢

热点阅读