使用 apache commons codec 编码解码
Apache Commons Codec jar包官方下载地址
http://commons.apache.org/proper/commons-codec/download_codec.cgi
下载解压后把commons-codec-1.9.jar 放到lib中
apache common codec 使用实例如下
import org.apache.commons.codec.binary.Base64;
public class Base64Test {
public static void main(String[] args) {
//使用Apache Commons Codec 编码解码
//替代sun.misc.BASE64Decoder 和 sun.misc.BASE64Encoder
//编码
String str = "中文";
byte[] enbytes = null;
String encodeStr = null;
Base64 base64 = new Base64();
enbytes = base64.encode(str.getBytes());
encodeStr = new String(enbytes);
System.out.println("编码前:" + str);
System.out.println("编码后:" + encodeStr);
System.out.println("-------------------------");
//解码
String enStr ="5Lit5paH";
Base64 base642 = new Base64();
byte[] bDecode = base642.decode(enStr);
String deStr = new String(bDecode);
System.out.println("解码前:" + enStr);
System.out.println("解码后:" + deStr);
}
}
建议不要用sun.misc,是不安全的 ,最好不要使用
使用apache common中的代替,使用sun.misc的BASE64在ant编译的时候会出现警告信息
warning: BASE64Decoder is internal proprietary API and may be removed in a future release warning: BASE64Encoder is internal proprietary API and may be removed in a future release
来源://作者:/更新时间:2014-04-24
顶
踩
相关文章:


![7 构建 java开发环境 [java入门教程]](/d/file/java/Javajichu/2012-11-07/a96295ae51e2ee68b9865e70b69d1346.jpg)
![5 Java 如何编译运行 [java入门教程]](/d/file/java/Javajichu/2012-11-07/f1f3ca66ea936e4761a614bd31471eb5.jpg)