Apache Commons Codec jar包官方下载地址
下载解压后把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