package org.nhooo.example.commons.codec; import org.apache.commons.codec.binary.Base64; import java.util.Arrays; public class Base64Decode { public static void main(String[] args) { String hello = "SGVsbG8gV29ybGQ="; // 使用DecodeBase64方法解码先前编码的字符串,然后 // 传递编码字符串的byte []。 byte[] decoded = Base64.decodeBase64(hello.getBytes()); // 打印解码后的数组 System.out.println(Arrays.toString(decoded)); // 将解码的byte []转换回原始字符串并打印 // 结果。 String decodedString = new String(decoded); System.out.println(hello + " = " + decodedString); } }
我们的代码的结果是:
[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] SGVsbG8gV29ybGQ= = Hello World
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>