Base64解码
Base64概述:
Base64是JDK8提出的一个新特性,可以用来进行按照一定规则编码和解码
使用:
编码:
1.获取编码器
2.对数据进行编码
解码:
1.获取解码器
2.对数据进行解码
Base64工具类提供了一套静态方法获取三种Base64编码解码
基本:输出被映射到一组字符A-Za-z0-9+/,编码不添加任何行标,输出的解码仅支持A-Za-z0-9+/
URL:输出被映射到一组字符A-Za-z0-9+_,输出是URL和文件
MIME:输出映射到MIME友好格式,因为输出每行数据不超过67个字符
API:
public static Encoder getEncoder():基本型 base64 编码器
public static Decoder getDecoder():基本型 base64 编码器
public static Encoder getUrlEncoder():Url型 base64 编码器
public static Decoder getUrlDecoder():Url型 base64 编码器
public static Encoder getMimeEncoder():Mime型 base64 编码器
public static Decoder getMimeDecoder():Mime型 base64 编码器
一、基本型
public static void main (String[] args) {// 使用基本型的编码器和解码器 对数据进行编码和解码// 1.获取编码器Base64.Encoder encoder = Base64.getEncoder();// 2.对字符串进行编码String str = "name=zhangsan&password==123456";String s = encoder.encodeToString(str.getBytes());// 3.输出编码后的字符串System.out.println("编码后的字符串:" + s);// 4.获取解码器Base64.Decoder decoder = Base64.getDecoder();// 5.对编码后的字符串进行解码byte[] decode = decoder.decode(s);String s1 = new String(decode);// 6.打印输出解码后的字符串System.out.println("解码后的字符串:" + s1);}二、URL型
public static void main (String[] args) {// 使用URl型的编码器和解码器 对数据进行编码和解码// 1.获取编码器Base64.Encoder encoder = Base64.getUrlEncoder();// 2.对字符串进行编码String str = "name=zhangsan&password==123456";String s = encoder.encodeToString(str.getBytes());// 3.输出编码后的字符串System.out.println("编码后的字符串:" + s);// 4.获取解码器Base64.Decoder decoder = Base64.getUrlDecoder();// 5.对编码后的字符串进行解码byte[] decode = decoder.decode(s);String s1 = new String(decode);// 6.打印输出解码后的字符串System.out.println("解码后的字符串:" + s1);}三、Mime型
public static void main (String[] args) {// 使用MIME型的编码器和解码器 对数据进行编码和解码// 1.获取编码器Base64.Encoder encoder = Base64.getMimeEncoder();// 2.对字符串进行编码String str = "name=zhangsan&password==123456";String s = encoder.encodeToString(str.getBytes());// 3.输出编码后的字符串System.out.println("编码后的字符串:" + s);// 4.获取解码器Base64.Decoder decoder = Base64.getMimeDecoder();// 5.对编码后的字符串进行解码byte[] decode = decoder.decode(s);String s1 = new String(decode);// 6.打印输出解码后的字符串System.out.println("解码后的字符串:" + s1);}总结
- 上一篇: 数学建模【三款超实用建模小软件!】
- 下一篇: PreferenceActivity之