Base64编码
java.util.Base64
package com.mozq.sb.jwt01.config;
import java.nio.charset.Charset;
import java.util.Base64;
/**
* @description: Base64编码
*/
public class Base64Test {
public static void main(String[] args) {
//将字节数组编码成Base64字符串
String src = "jie";
String enc = Base64.getEncoder().encodeToString(src.getBytes());
//不指定编码会使用默认的编码 Charset.defaultCharset().name()
System.out.println(enc);
//将Base64字符串解码成字节数组
byte[] decode = Base64.getDecoder().decode(enc);
String aSrc = new String(decode);
System.out.println(aSrc);
String defaultCharset = Charset.defaultCharset().name();
System.out.println(defaultCharset);//UTF-8
}
}
commons-codec
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
package com.mozq.sb.jwt01.config;
import org.apache.commons.codec.binary.Base64;
import java.nio.charset.Charset;
/**
* @description: Base64编码
*/
public class Base64Test_02 {
public static void main(String[] args) {
//将字节数组编码成Base64字符串
String src = "jie//++--\";
String enc = new Base64().encodeToString(src.getBytes());//amllLy8rKy0tXA==
//不指定编码会使用默认的编码 Charset.defaultCharset().name()
System.out.println(enc);
//将Base64字符串解码成字节数组
byte[] decode = new Base64().decode(enc);
String aSrc = new String(decode);
System.out.println(aSrc);
String defaultCharset = Charset.defaultCharset().name();
System.out.println(defaultCharset);//UTF-8
}
}