java中的BASE64的编码和解码

import java.io.IOException;
 
public class Base64Test {
 
 
//编码
public static String encode(byte[] bstr) {
return new sun.misc.BASE64Encoder().encode(bstr);
}
 
//解码
public static byte[] decode(String str) {
byte[] bt = null;
try {
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
bt = decoder.decodeBuffer(str);
} catch (IOException e) {
e.printStackTrace();
}
return bt;
}
 
//测试
public static void main(String[] args) {
String aa = "美好的一天";
System.out.println("测试字符串:" + aa);
 
aa = Base64Test.encode(aa.getBytes());
System.out.println("编码后:" + aa);
 
String str = new String(Base64Test.decode(aa));
System.out.println("解码后:" + str);
}
}

你可能感兴趣的:(java,sun)