JDK6 Base64 编码解码:使用 Apache Commons Codec 库
在 JDK6 中,可以使用 Apache Commons Codec 库来实现 Base64 编码和解码的功能。
具体实现如下:
- 添加依赖
在项目中添加 Apache Commons Codec 的依赖:
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
- 编码
使用 Base64.encodeBase64String 方法对字节数组进行编码:
import org.apache.commons.codec.binary.Base64;
public class Base64Util {
public static String encode(byte[] data) {
return Base64.encodeBase64String(data);
}
}
- 解码
使用 Base64.decodeBase64 方法对 Base64 字符串进行解码:
import org.apache.commons.codec.binary.Base64;
public class Base64Util {
public static byte[] decode(String base64) {
return Base64.decodeBase64(base64);
}
}
原文地址: https://www.cveoy.top/t/topic/oe2t 著作权归作者所有。请勿转载和采集!