java.util.Base64测试

在Java 8中,Base64编码已经成为Java类库的标准。

Base64工具类提供了一套静态方法获取下面三种BASE64编解码器:基本、URL、MIME。

 

1、基本:

 1 @Test
 2 public void testBase64() throws UnsupportedEncodingException {
 3     String srcStr = "-`测试Base64_$(#)*^》/,。;\"'";
 4     // 编码    A-Za-z0-9+/
 5     String base64encodedString = Base64.getEncoder().encodeToString(srcStr.getBytes("utf-8"));
 6     System.out.println(base64encodedString);  // LWDmtYvor5VCYXNlNjRfJO+8iCPvvIkqXuOAiy/vvIzjgILvvJsiJw==
 7     // 执行解码
 8     String base64decodedString = new String(Base64.getDecoder().decode(base64encodedString), "utf-8");
 9     // 校验数据经过编码解码后是否依然完整
10     System.out.println(srcStr.equals(base64decodedString)); // true
11 }

 

2、URL:

 1 @Test
 2 public void testBase64URL() throws UnsupportedEncodingException {
 3     String srcStr = "172.20.14.93:8080/console/logout?SAMLRequest=true&p=哈是&service=http%3A%2F%2F172.20.14.93%3A800";
 4     // 编码
 5     String base64encodedString = Base64.getUrlEncoder().encodeToString(srcStr.getBytes("utf-8"));
 6     System.out.println(base64encodedString);  
 7     // out: MTcyLjIwLjE0LjkzOjgwODAvY29uc29sZS9sb2dvdXQ_U0FNTFJlcXVlc3Q9dHJ1ZSZwPeWTiOaYryZzZXJ2aWNlPWh0dHAlM0ElMkYlMkYxNzIuMjAuMTQuOTMlM0E4MDA=
 8     // 执行解码
 9     String base64decodedString = new String(Base64.getUrlDecoder().decode(base64encodedString), "utf-8");
10     // 校验数据经过编码解码后是否依然完整
11     System.out.println(srcStr.equals(base64decodedString)); // true
12 }

 

3、MIME:

 1 @Test
 2 public void testBase64MIME() throws UnsupportedEncodingException {
 3     String srcStr = "在Java 8中,Base64编码已经成为Java类库的标准。"
 4             + "Java 8 内置了 Base64 编码的编码器和解码器。"
 5             + "Base64工具类提供了一套静态方法获取下面三种BASE64编解码器:"
 6             + "基本:输出被映射到一组字符A-Za-z0-9+/,编码不添加任何行标,输出的解码仅支持A-Za-z0-9+/。"
 7             + "URL:输出映射到一组字符A-Za-z0-9+_,输出是URL和文件。"
 8             + "MIME:输出隐射到MIME友好格式。输出每行不超过76字符,并且使用'\r'并跟随'\n'作为分割。"
 9             + "编码输出最后没有行分割。";
10     // 编码
11     String base64encodedString = Base64.getMimeEncoder().encodeToString(srcStr.getBytes("utf-8"));
12     System.out.println(base64encodedString); 
13     // 执行解码
14     String base64decodedString = new String(Base64.getMimeDecoder().decode(base64encodedString), "utf-8");
15     // 校验数据经过编码解码后是否依然完整
16     System.out.println(srcStr.equals(base64decodedString)); // true
17 } /*out:
18 5ZyoSmF2YSA45Lit77yMQmFzZTY057yW56CB5bey57uP5oiQ5Li6SmF2Yeexu+W6k+eahOagh+WH
19 huOAgkphdmEgOCDlhoXnva7kuoYgQmFzZTY0IOe8lueggeeahOe8lueggeWZqOWSjOino+eggeWZ
20 qOOAgkJhc2U2NOW3peWFt+exu+aPkOS+m+S6huS4gOWll+mdmeaAgeaWueazleiOt+WPluS4i+md
21 ouS4ieenjUJBU0U2NOe8luino+eggeWZqO+8muWfuuacrO+8mui+k+WHuuiiq+aYoOWwhOWIsOS4
22 gOe7hOWtl+espkEtWmEtejAtOSsv77yM57yW56CB5LiN5re75Yqg5Lu75L2V6KGM5qCH77yM6L6T
23 5Ye655qE6Kej56CB5LuF5pSv5oyBQS1aYS16MC05Ky/jgIJVUkzvvJrovpPlh7rmmKDlsITliLDk
24 uIDnu4TlrZfnrKZBLVphLXowLTkrX++8jOi+k+WHuuaYr1VSTOWSjOaWh+S7tuOAgk1JTUXvvJro
25 vpPlh7rpmpDlsITliLBNSU1F5Y+L5aW95qC85byP44CC6L6T5Ye65q+P6KGM5LiN6LaF6L+HNzbl
26 rZfnrKbvvIzlubbkuJTkvb/nlKgnDSflubbot5/pmo8nCifkvZzkuLrliIblibLjgILnvJbnoIHo
27 vpPlh7rmnIDlkI7msqHmnInooYzliIblibLjgII=
28 */

 

 

posted @ 2019-05-23 15:45  渣渣伟  阅读(3902)  评论(0编辑  收藏  举报
--->