java commons codec_使用 apache commons codec 编码解码

本文介绍了Apache Commons Codec jar包的官方下载地址,下载解压后将commons - codec - 1.9.jar放入lib中。给出了使用实例,展示编码解码过程。同时建议不要使用sun.misc的BASE64相关类,因其不安全,且ant编译时会有警告信息,最好用Apache Commons Codec替代。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值