如下所示:
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.tuohang.net/t/base64/
以上这篇java jdk1.7对字符串的base64编码解码方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
原文链接:https://blog.csdn.net/u013511642/article/details/80344870
查看更多关于Java JDK1.7对字符串的BASE64编码解码方法的详细内容...