如下所示:
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
|
package cn.itcast; import java.io.ioexception; import java.io.unsupportedencodingexception; import org.junit.test; import sun.misc.base64decoder; /* * @author soto * base64編碼 解碼 * */ public class demo1 { @test public void fun1() throws ioexception{ //base64編碼 string str = "hello" ; byte [] bytes = str.getbytes( "utf-8" ); str = new sun.misc.base64encoder().encode(bytes); system.out.println( "編碼后... " +str); //base64解碼 base64decoder decoder = new base64decoder(); byte [] b = decoder.decodebuffer(str); str = new string(b, "utf-8" ); system.out.println( "解碼后... " + str); } } |
注意:在jdk1.8 中 的jutil包中加入了新的base64解碼編碼方式,使得編解碼更簡單。
PS:推薦一款Base64在線編碼解碼工具:https://tool.zzvips.com/t/base64/
以上這篇java jdk1.7對字符串的base64編碼解碼方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/u013511642/article/details/80344870