Java字符串Base64解码
在Java中,可以使用java.util.Base64类来解码Base64编码的字符串,以下是几种实现方法:

方法1:使用Java 8+内置的Base64类
import java.util.Base64;
import java.nio.charset.StandardCharsets;
public class Base64Decoder {
public static String decode(String base64String) {
// 解码Base64字符串
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
// 将字节数组转换为字符串
return new String(decodedBytes, StandardCharsets.UTF_8);
}
public static void main(String[] args) {
String encodedString = "SGVsbG8gV29ybGQh"; // "Hello World!"的Base64编码
String decodedString = decode(encodedString);
System.out.println("解码结果: " + decodedString);
}
}
方法2:处理包含换行符的Base64字符串
如果Base64字符串包含换行符或其他空白字符,可以先清理:
import java.util.Base64;
import java.nio.charset.StandardCharsets;
public class Base64Decoder {
public static String decodeWithClean(String base64String) {
// 移除所有空白字符
String cleaned = base64String.replaceAll("\\s+", "");
// 解码
byte[] decodedBytes = Base64.getDecoder().decode(cleaned);
return new String(decodedBytes, StandardCharsets.UTF_8);
}
}
方法3:处理URL安全的Base64编码
URL安全的Base64编码使用和_代替和:
import java.util.Base64;
import java.nio.charset.StandardCharsets;
public class Base64Decoder {
public static String decodeUrlSafe(String urlSafeBase64) {
// 使用URL安全的解码器
byte[] decodedBytes = Base64.getUrlDecoder().decode(urlSafeBase64);
return new String(decodedBytes, StandardCharsets.UTF_8);
}
}
方法4:使用Apache Commons Codec库
如果你更喜欢使用第三方库,可以添加Apache Commons Codec依赖:
<!-- Maven依赖 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
然后使用:

import org.apache.commons.codec.binary.Base64;
import java.nio.charset.StandardCharsets;
public class Base64Decoder {
public static String decodeWithApache(String base64String) {
byte[] decodedBytes = Base64.decodeBase64(base64String);
return new String(decodedBytes, StandardCharsets.UTF_8);
}
}
注意事项
- 确保输入的Base64字符串是有效的,否则会抛出
IllegalArgumentException - 解码后的字节转换为字符串时,要指定正确的字符编码(通常是UTF-8)
- 对于二进制数据解码,不需要转换为字符串,可以直接使用字节数组
完整示例
import java.util.Base64;
import java.nio.charset.StandardCharsets;
public class Base64DecoderExample {
public static void main(String[] args) {
// 标准Base64编码
String standardEncoded = "SGVsbG8gV29ybGQh";
System.out.println("标准解码: " + decode(standardEncoded));
// URL安全编码
String urlSafeEncoded = "SGVsbG8gV29ybGQ-";
System.out.println("URL安全解码: " + decodeUrlSafe(urlSafeEncoded));
// 带换行符的编码
String withNewlines = "SGVsbG8g\nV29ybGQh";
System.out.println("带换行符解码: " + decodeWithClean(withNewlines));
}
// 标准解码方法
public static String decode(String base64String) {
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
return new String(decodedBytes, StandardCharsets.UTF_8);
}
// URL安全解码方法
public static String decodeUrlSafe(String urlSafeBase64) {
byte[] decodedBytes = Base64.getUrlDecoder().decode(urlSafeBase64);
return new String(decodedBytes, StandardCharsets.UTF_8);
}
// 清理空白字符解码方法
public static String decodeWithClean(String base64String) {
String cleaned = base64String.replaceAll("\\s+", "");
byte[] decodedBytes = Base64.getDecoder().decode(cleaned);
return new String(decodedBytes, StandardCharsets.UTF_8);
}
}
方法可以根据你的具体需求选择使用,对于现代Java应用(Java 8及以上),推荐使用内置的java.util.Base64类。

