Base64 简介
首先上代码
package base64;
import org.junit.Test;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/** 原生jdk实现*/
public class Base64Test {
private static final String UTF8 = StandardCharsets.UTF_8.name();
@Test
public void test1() throws Exception{
String helloWorld = "你好,世界";
String encoder = Base64.getEncoder().encodeToString(helloWorld.getBytes(UTF8));
System.out.println("encoder:"+encoder);
byte[] decode = Base64.getDecoder().decode(encoder.getBytes(UTF8));
String decodeStr = new String(decode, UTF8);
System.out.println("decode:"+decodeStr);
}
}
结果解析
encoder:5L2g5aW977yM5LiW55WM
decode:你好,世界
===============================
base64:5L2g5aW977yM5LiW55WM 这些字符是A-Z、a-z、0-9、+、/这64个字母中的,base64以三组字节为一组,如果不足三个字节,用‘=’补充
Base64 的作用
不是为了加密和解密,是为了更好的在网络中传输数据(将二进制转换成base64),或者是为了本地存储字节数组。

浙公网安备 33010602011771号