【笔记】Java实现Base64编解码

前言

Java实现Base64编解码

通过JDK8提供的类实现Base64编解码

基本编解码器

Base64编码

1
String result = Base64.getEncoder().encodeToString("未编码的字符串".getBytes("utf-8"));

Base64解码

1
String result = Base64.getDecoder().decode("已编码的字符串");

针对于URL和文件名安全的编解码器

Base64编码

1
String result = Base64.getUrlEncoder().encodeToString("未编码的字符串".getBytes("utf-8"));

Base64解码

1
String result = Base64.getUrlDecoder().decode("已编码的字符串");

通过apache提供的类实现Base64编解码

引入依赖

1
import org.apache.commons.codec.binary.Base64;

Base64编码

1
String result = Base64.encodeBase64String("未编码的字符串".getBytes());

Base64解码

1
String result = new String(Base64.decodeBase64("已编码的字符串"));

完成

参考文献

CSDN——多来哈米