JAVA实现BASE64编码与解码
时间:2010-06-06 来源:hkebao
package cn.test;
import sun.misc.BASE64Decoder;
public class Test {
public static void main(String[] args) {
System.out.println(getBASE64("fffdsd"));
System.out.println(getFormBASE64("ZmZmZHNk"));
}
public static String getBASE64(String s) {
if (s==null) return null;
return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
}
// 将BASE64编码的字符进行解码处理
public static String getFormBASE64(String s) {
if (s == null) {
return null;
}
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) {
return null;
}
}
}
public static void main(String[] args) {
System.out.println(getBASE64("fffdsd"));
System.out.println(getFormBASE64("ZmZmZHNk"));
}
public static String getBASE64(String s) {
if (s==null) return null;
return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
}
// 将BASE64编码的字符进行解码处理
public static String getFormBASE64(String s) {
if (s == null) {
return null;
}
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) {
return null;
}
}
}
相关阅读 更多 +