package org.nhooo.example.commons.codec;
import org.apache.commons.codec.binary.Base64;
import java.util.Arrays;
public class Base64Encode {
public static void main(String[] args) {
String hello = "Hello World";
//encodeBase64方法采用byte []作为参数。字节[]
// 可以来自本示例中的简单字符串,也可以来自
// 图像文件数据。
byte[] encoded = Base64.encodeBase64(hello.getBytes());
// 打印编码的字节数组
System.out.println(Arrays.toString(encoded));
// 打印编码的字符串
String encodedString = new String(encoded);
System.out.println(hello + " = " + encodedString);
}
}我们程序的结果:
[83, 71, 86, 115, 98, 71, 56, 103, 86, 50, 57, 121, 98, 71, 81, 61] Hello World = SGVsbG8gV29ybGQ=
Maven依赖
<!-- https://search.maven.org/remotecontent?filepath=commons-codec/commons-codec/1.12/commons-codec-1.12.jar --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.12</version> </dependency>