展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

Base64 api

  • jdk8之前
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Main {

    public static void main(String[] args) throws Exception {
        BASE64Encoder encoder = new BASE64Encoder();
        BASE64Decoder decoder = new BASE64Decoder();
        String text = "小滴课堂";
        byte[] textByte = text.getBytes("UTF-8");
        //编码
        String encodedText = encoder.encode(textByte);
        System.out.println(encodedText);
        //解码
        System.out.println(new String(decoder.decodeBuffer(encodedText),"UTF-8"));
    }

}
  • jdk8之后
import java.util.Base64;

public class Main {

    public static void main(String[] args) throws Exception {
        Base64.Encoder  encoder  =  Base64.getEncoder();
        Base64.Decoder decoder = Base64.getDecoder();
        String text = "小滴课堂";
        byte[] textByte = text.getBytes("UTF-8");
        //编码
        String encodedText = encoder.encodeToString(textByte);
        System.out.println(encodedText);
        //解码
        System.out.println(new String(decoder.decode(encodedText),"UTF-8"));
    }

}
posted @ 2022-08-23 16:45  DogLeftover  阅读(53)  评论(0编辑  收藏  举报