返回类型 方法描述
static Base64.Decoder getDecoder()

返回Base64.Decoder解码使用Basic型base64编码方案。
static Base64.Encoder getEncoder()

返回一个Base64.Encoder编码使用Basic型base64编码方案。
static Base64.Decoder getMimeDecoder()

返回一个Base64.Decoder解码使用MIME型BASE64解码方案。
static Base64.Encoder getMimeEncoder()

返回一个Base64.Encoder编码使用MIME型base64编码方案。
static Base64.Encoder getMimeEncoder(int lineLength, byte[] lineSeparator)

返回一个Base64.Encoder ,它使用具有指定行长度和行分隔符的MIME类型base64编码方案进行编码。
static Base64.Decoder getUrlDecoder()

返回Base64.Decoder解码使用URL and Filename safe型base64编码方案。
static Base64.Encoder getUrlEncoder()

返回一个Base64.Encoder编码使用URL and Filename safe型base64编码方案。


Class Base64.Decoder

该类使用RFC 4648和RFC 2045中规定的Base64编码方案来实现用于解码字节数据的解码器

返回类型 方法描述
byte[] decode(byte[] src)

使用Base64编码方案从输入字节数组中解码所有字节,将结果写入新分配的输出字节数组。
int decode(byte[] src, byte[] dst)

使用Base64编码方案从输入字节数组中解码所有字节,将结果写入给定的输出字节数组,从偏移0开始。
ByteBuffer decode(ByteBuffer buffer)

使用Base64编码方案从输入字节缓冲区中解码所有字节,将结果写入新分配的ByteBuffer。
byte[] decode(String src)

使用Base64编码方案将Base64编码的字符串解码为新分配的字节数组。
InputStream wrap(InputStream is)

返回一个输入流,用于解码Base64编码字节流。


Class Base64.Encoder

该类使用RFC 4648和RFC 2045中规定的Base64编码方案来实现用于编码字节数据的编码器

返回类型 方法描述
byte[] encode(byte[] src)

使用Base64编码方案将指定字节数组中的所有字节编码为新分配的字节数组。
int encode(byte[] src, byte[] dst)

使用Base64编码方案对来自指定字节数组的所有字节进行编码,将生成的字节写入给定的输出字节数组,从偏移0开始。
ByteBuffer encode(ByteBuffer buffer)

使用Base64编码方案将所有剩余字节从指定的字节缓冲区编码到新分配的ByteBuffer中。
String encodeToString(byte[] src)

使用Base64编码方案将指定的字节数组编码为字符串。
Base64.Encoder withoutPadding()

返回一个编码器实例,编码器等效于此编码器实例,但不会在编码字节数据的末尾添加任何填充字符。
OutputStream wrap(OutputStream os)

使用Base64编码方案包装用于编码字节数据的输出流。


示例:

使用基本型的编码器和解码器:
String str = "ok"; //定义一个字符串
Base64.Encoder encoder = Base64.getEncoder(); //创建编码器
String s = encoder.encodeToString(str.getBytes("UTF-8")); //对字符串进行编码

Base64.Decoder decoder = Base64.getDecoder(); //创建解码器
byte[] decode = decoder.decode(s); //对编码后的字符串进行解码
String s1 = new String(decode, "UTF-8");



使用URL型的编码器和解码器:
String str = "name=zhangsan&password=123456"; //定义一个url型字符串
Base64.Encoder encoder = Base64.getUrlEncoder(); //创建URL编码器
String s = encoder.encodeToString(str.getBytes("UTF-8")); //对字符串进行编码

Base64.Decoder decoder = Base64.getUrlDecoder(); //创建URL解码器
byte[] decode = decoder.decode(s); //对编码后的字符串进行解码
String s1 = new String(decode, "UTF-8");