<%@ page import="sun.misc.BASE64Decoder" %>
<%@ page import="sun.misc.BASE64Encoder" %>
// 将 s 进行 BASE64 编码
static String getBASE64(String s) {
if (s == null) return null;
return (new sun.misc.BASE64Encoder()).encode( s.getBytes() );
}
// 将 BASE64 编码的字符串 s 进行解码
static String decodeBASE64(String ss) {
BASE64Decoder decoder = new BASE64Decoder();
if(ss == null){
return null;
}else{
try{
byte[] b = decoder.decodeBuffer(ss);
return new String(b);
}catch(Exception e){
return null;
}
}
}
}
<%@ page import="sun.misc.BASE64Encoder" %>
// 将 s 进行 BASE64 编码
static String getBASE64(String s) {
if (s == null) return null;
return (new sun.misc.BASE64Encoder()).encode( s.getBytes() );
}
// 将 BASE64 编码的字符串 s 进行解码
static String decodeBASE64(String ss) {
BASE64Decoder decoder = new BASE64Decoder();
if(ss == null){
return null;
}else{
try{
byte[] b = decoder.decodeBuffer(ss);
return new String(b);
}catch(Exception e){
return null;
}
}
}
}
本文介绍了一种使用Java实现BASE64编码与解码的方法。通过具体的代码示例展示了如何将字符串转换为BASE64编码形式,以及如何将已编码的BASE64字符串还原回原始字符串。
1851

被折叠的 条评论
为什么被折叠?



