1. 普通编码
小段文字
2. URL编码
多数用于传输地址
3. MIME编码
多数用于传输实体内部类、大段文字/邮箱、图片文件
public class Base64Demo {
public static void main(String args[]) {
try {
// 1-1.基本编码后结果。普通文本的编码
String rs1 = Base64.getEncoder().encodeToString("黑马程序员".getBytes());
System.out.println(rs1); // 6buR6ams56iL5bqP5ZGY
// 1-2.基本解码后结果。普通文本的解码
byte[] buffer = Base64.getDecoder().decode(rs1);
System.out.println(new String(buffer));
// 2-1.URL编码
String rs2 = Base64.getUrlEncoder().encodeToString("?loginName=黑马&passWord=123456".getBytes());
System.out.println(rs2);
// 2-2 URL解码
byte[] buffer2 = Base64.getUrlDecoder().decode(rs2);
System.out.println(new String(buffer2));
// 3-1 MIME编码
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; ++i) {
sb.append(UUID.randomUUID().toString());
}
String rs3 = Base64.getMimeEncoder().encodeToString(sb.toString().getBytes());
System.out.println(rs3);
// 3-2 MIME解码
byte[] buffer3 = Base64.getMimeDecoder().decode(rs3);
System.out.println(new String(buffer3));
}catch(Exception e){
System.out.println("Error :" + e.getMessage());
}
}
}
标签:10,String,编码,Base64,System,println,out
From: https://www.cnblogs.com/Gege-1/p/17138872.html