java中base64
2013-10-15 19:18
375 查看
// 将 s 进行 BASE64 编码
public static String getBASE64(String s) {
if (s == null) return null;
return (new sun.misc.BASE64Encoder()).encode( s.getBytes() );
}
// 将 BASE64 编码的字符串 s 进行解码
public static String getFromBASE64(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;
}
}
或者apache的包
public static String getBASE64(String s) {
if (s == null) return null;
return (new sun.misc.BASE64Encoder()).encode( s.getBytes() );
}
// 将 BASE64 编码的字符串 s 进行解码
public static String getFromBASE64(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;
}
}
或者apache的包
import java.io.UnsupportedEncodingException; import org.apache.commons.codec.binary.Base64; public class Base64Util { /** * 将二进制数据编码为BASE64字符串 * @param binaryData * @return */ public static String encode(byte[] binaryData) { try { return new String(Base64.encodeBase64(binaryData), "UTF-8"); } catch (UnsupportedEncodingException e) { return null; } } /** * 将BASE64字符串恢复为二进制数据 * @param base64String * @return */ public static byte[] decode(String base64String) { try { return Base64.decodeBase64(base64String.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { return null; } } }
相关文章推荐
- java中的多线程编程
- java 调用dll之jni(1)
- javascricpt 中Array数组的(last)indexOf(searching[,(startForm)]) 中startForm的详解
- Java与Java2的区别
- java通过反射调用不同参数的方法
- [置顶] 我的设计模式学习笔记------>Java设计模式总概况
- Java2平台J2SE、J2EE、J2ME三大版本的区别
- Java中Split函数的用法技巧
- 对Spring.Net的AOP一些思考及应用
- eclipse 安装软件
- 常用的Struts 2.0的标志(Tag)介绍
- JDK 1.5 1.6 1.7特性
- 对Spring.Net的AOP一些思考及应用
- JAVA发送邮件工具类
- 关于JAVA的守护进程
- java异常处理
- Eclipse使用技巧及快捷键大全(精品集合)
- java中如何实现邮件的发送包括网页文件的发送
- JDK,JRE,JVM区别与联系
- spring中c3p0配置 ---这是一位仁兄的经历