在java 8之前如果需要使用base64编解码,必须使用三方库,如:apache的commons-codec。

但是java 8将base64编解码的工具引入进来:

public class TestBase64 {

    public static void main(String[] args) {
        String plainTxt = "i love you!";
        String secure = base64Encode(plainTxt);
        base64Decode(secure);
    }


    public static String base64Encode(String plainTxt) {
        byte[] bytes = Base64.getEncoder().encode(plainTxt.getBytes());
        String secureTxt = new String(bytes);
        System.out.println(secureTxt);
        return secureTxt;
    }

    public static void base64Decode(String secureTxt) {
        byte[] bytes = Base64.getDecoder().decode(secureTxt.getBytes());
        String plainTxt = new String(bytes);
        System.out.println(plainTxt);
    }
}

执行结果:

aSBsb3ZlIHlvdSE=
i love you!
参考

What's New in JDK 8

相关文章:

  • 2021-12-29
  • 2021-07-28
  • 2022-12-23
  • 2021-11-14
  • 2021-08-10
  • 2021-11-07
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
相关资源
相似解决方案