Java Base64
JDK中的Base64演化流程
Base64就是用来将非ASCII字符的数据转换成ASCII字符的一种方法。
在JDK1.6之前,JDK核心类一直没有Base64的实现类。一般通过反射调用 Sun/Oracle JDK 里面的 sun.misc.BASE64Encoder 和 sun.misc.BASE64Decoder,使用它的优点就是不需要依赖第三方类库,缺点就是可能在未来版本会被删除(用maven编译会发出警告),而且性能不佳。
JDK1.6中添加了另一个Base64的实现,javax.xml.bind.DatatypeConverter两个静态方法parseBase64Binary 和 printBase64Binary,隐藏在javax.xml.bind包下面。
在Java 8在java.util包下面实现了Base64编解码API,而且性能不俗,API也简单易懂。该Base64有3个Encoder,分别是“标准Encoder”、“urlEncoder”、“mimeEncoder”,它们编码出的字符串基本相同,只有在原始字符串是以“某种协议://……”开头时,后续的问号(”?”)编码的结果才会有所不同 —— “标准Encoder”和“mimeEncoder”将其编码为“/”,而“urlEncoder”却将编码为“_”。其它情况发现编码出的结果暂时都是一样的。
测试代码
package com.demo.test;
import java.util.Arrays;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.UUID;
public class Base64Demo {
public static void main(String[] args) {
String str = "~!@#$%^&*()_+{}:\"|<>?`-=[];'\\,./'";
printBase64Str(str);
str = "http://localhost:8080/index.html?word1=xxx&word2=xxx";
printBase64Str(str);
str = "http://localhost:8080/index.html?";
printBase64Str(str);
str = "file://localhost:8080/index.html?";
printBase64Str(str);
str = "file:///localhost:8080/index.html?";
printBase64Str(str);
str = "localhost:8080/index.html?";
printBase64Str(str);
str = UUID.randomUUID().toString();
printBase64Str(str);
str = "中文测试";
printBase64Str(str);
}
private static void printBase64Str(String str) {
System.out.println(str);
byte[] buffer = str.getBytes();
Encoder encoder = Base64.getEncoder();
Encoder urlEncoder = Base64.getUrlEncoder();
Encoder mimeEncoder = Base64.getMimeEncoder();
String result = encoder.encodeToString(buffer);
String urlResult = urlEncoder.encodeToString(buffer);
String mimeResult = mimeEncoder.encodeToString(buffer);
System.out.println(result);
System.out.println(urlResult);
System.out.println(mimeResult);
if(!Arrays.equals(buffer, Base64.getDecoder().decode(result))) {
System.err.println("decode not equal");
}
if(!Arrays.equals(buffer, Base64.getUrlDecoder().decode(urlResult))) {
System.err.println("decode not equal");
}
if(!Arrays.equals(buffer, Base64.getMimeDecoder().decode(mimeResult))) {
System.err.println("decode not equal");
}
System.out.println();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
运行输出
~!@#$%^&*()_+{}:"|<>?`-=[];'\,./'
fiFAIyQlXiYqKClfK3t9OiJ8PD4/YC09W107J1wsLi8n
fiFAIyQlXiYqKClfK3t9OiJ8PD4_YC09W107J1wsLi8n
fiFAIyQlXiYqKClfK3t9OiJ8PD4/YC09W107J1wsLi8n
http://localhost:8080/index.html?word1=xxx&word2=xxx
aHR0cDovL2xvY2FsaG9zdDo4MDgwL2luZGV4Lmh0bWw/d29yZDE9eHh4JndvcmQyPXh4eA==
aHR0cDovL2xvY2FsaG9zdDo4MDgwL2luZGV4Lmh0bWw_d29yZDE9eHh4JndvcmQyPXh4eA==
aHR0cDovL2xvY2FsaG9zdDo4MDgwL2luZGV4Lmh0bWw/d29yZDE9eHh4JndvcmQyPXh4eA==
http://localhost:8080/index.html?
aHR0cDovL2xvY2FsaG9zdDo4MDgwL2luZGV4Lmh0bWw/
aHR0cDovL2xvY2FsaG9zdDo4MDgwL2luZGV4Lmh0bWw_
aHR0cDovL2xvY2FsaG9zdDo4MDgwL2luZGV4Lmh0bWw/
file://localhost:8080/index.html?
ZmlsZTovL2xvY2FsaG9zdDo4MDgwL2luZGV4Lmh0bWw/
ZmlsZTovL2xvY2FsaG9zdDo4MDgwL2luZGV4Lmh0bWw_
ZmlsZTovL2xvY2FsaG9zdDo4MDgwL2luZGV4Lmh0bWw/
file:///localhost:8080/index.html?
ZmlsZTovLy9sb2NhbGhvc3Q6ODA4MC9pbmRleC5odG1sPw==
ZmlsZTovLy9sb2NhbGhvc3Q6ODA4MC9pbmRleC5odG1sPw==
ZmlsZTovLy9sb2NhbGhvc3Q6ODA4MC9pbmRleC5odG1sPw==
localhost:8080/index.html?
bG9jYWxob3N0OjgwODAvaW5kZXguaHRtbD8=
bG9jYWxob3N0OjgwODAvaW5kZXguaHRtbD8=
bG9jYWxob3N0OjgwODAvaW5kZXguaHRtbD8=
d1a2e1be-6928-465a-b7c9-1d67e22d1536
ZDFhMmUxYmUtNjkyOC00NjVhLWI3YzktMWQ2N2UyMmQxNTM2
ZDFhMmUxYmUtNjkyOC00NjVhLWI3YzktMWQ2N2UyMmQxNTM2
ZDFhMmUxYmUtNjkyOC00NjVhLWI3YzktMWQ2N2UyMmQxNTM2
中文测试
1tDOxLLiytQ=
1tDOxLLiytQ=
1tDOxLLiytQ=
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/chy555chy/article/details/53672417
- 点赞
- 收藏
- 关注作者
评论(0)