public class Base64Util {
// 把普通字符编码成64bit
public static String strToBase64(String s)
throws UnsupportedEncodingException {
if (s == null)
return null;
return (new sun.misc.BASE64Encoder()).encode(s.getBytes("UTF-8"));
}
// 把64bit编码转化成普通字符串
public static String base64ToStr(String s) {
if (s == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return new String(b, "UTF-8");
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
String s = "我是中国人";
try {
System.out.println(StrToBase64(s));
System.out.println(Base64ToStr(StrToBase64(s)));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
项目中使用了的Base64方法
最新推荐文章于 2023-12-25 14:22:04 发布