minglipro 34dccd1895
All checks were successful
Gitea Actions Build / Build (push) Successful in 51s
feat(utils): 添加 AES 加密解密工具类
- 新增 AesUtils 类,提供 AES/GCM 模式的加密解密方法- 更新 .prettierrc配置,将 useTabs 设置为 true
- 重构 Base64Utils 和 Collection 类的代码结构- 简化 ComponentBean 注解定义
2025-08-17 19:12:08 +08:00

173 lines
4.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.mingliqiye.utils.base64;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
/**
* Base64工具类提供对字节数组、文件和字符串的Base64编码与解码功能。
*/
public class Base64Utils {
// Base64编码器实例
private static final Base64.Encoder BASE_64_ENCODER = Base64.getEncoder();
// Base64解码器实例
private static final Base64.Decoder BASE_64_DECODER = Base64.getDecoder();
/**
* 对字节数组进行Base64编码。
*
* @param bytes 待编码的字节数组
* @return 编码后的Base64字符串
*/
public static String encode(byte[] bytes) {
return BASE_64_ENCODER.encodeToString(bytes);
}
/**
* 对文件内容进行Base64编码。
*
* @param file 待编码的文件对象
* @return 编码后的Base64字符串
* @throws RuntimeException 如果读取文件时发生IO异常
*/
public static String encode(File file) {
try {
byte[] bytes = java.nio.file.Files.readAllBytes(file.toPath());
return encode(bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 根据文件路径对文件内容进行Base64编码。
*
* @param filePath 文件路径
* @return 编码后的Base64字符串
*/
public static String encode(String filePath) {
return encode(new File(filePath));
}
/**
* 安全地对文件内容进行Base64编码出错时返回null。
*
* @param file 待编码的文件对象
* @return 编码后的Base64字符串出错时返回null
*/
public static String encodeSafe(File file) {
try {
return encode(file);
} catch (Exception e) {
return null;
}
}
/**
* 安全地根据文件路径对文件内容进行Base64编码出错时返回null。
*
* @param filePath 文件路径
* @return 编码后的Base64字符串出错时返回null
*/
public static String encodeSafe(String filePath) {
try {
return encode(filePath);
} catch (Exception e) {
return null;
}
}
/**
* 对Base64字符串进行解码。
*
* @param base64 待解码的Base64字符串
* @return 解码后的字节数组
*/
public static byte[] decode(String base64) {
return BASE_64_DECODER.decode(base64);
}
/**
* 安全地对Base64字符串进行解码出错时返回null。
*
* @param base64 待解码的Base64字符串
* @return 解码后的字节数组出错时返回null
*/
public static byte[] decodeSafe(String base64) {
try {
return decode(base64);
} catch (Exception e) {
return null;
}
}
/**
* 将Base64字符串解码并写入指定文件。
*
* @param base64 待解码的Base64字符串
* @param file 目标文件对象
* @throws RuntimeException 如果写入文件时发生IO异常
*/
public static void decodeToFile(String base64, File file) {
try (FileOutputStream fos = new FileOutputStream(file)) {
byte[] bytes = decode(base64);
fos.write(bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 将Base64字符串解码并写入指定路径的文件。
*
* @param base64 待解码的Base64字符串
* @param filePath 目标文件路径
*/
public static void decodeToFile(String base64, String filePath) {
decodeToFile(base64, new File(filePath));
}
/**
* 安全地将Base64字符串解码并写入指定文件出错时返回false。
*
* @param base64 待解码的Base64字符串
* @param file 目标文件对象
* @return 成功写入返回true否则返回false
*/
public static boolean decodeToFileSafe(String base64, File file) {
try {
decodeToFile(base64, file);
return true;
} catch (Exception e) {
return false;
}
}
/**
* 安全地将Base64字符串解码并写入指定路径的文件出错时返回false。
*
* @param base64 待解码的Base64字符串
* @param filePath 目标文件路径
* @return 成功写入返回true否则返回false
*/
public static boolean decodeToFileSafe(String base64, String filePath) {
return decodeToFileSafe(base64, new File(filePath));
}
/**
* 对字节数组中指定范围的数据进行Base64编码。
*
* @param bytes 源字节数组
* @param offset 起始偏移量
* @param length 要编码的数据长度
* @return 编码后的Base64字符串
*/
public static String encodeBytes(byte[] bytes, int offset, int length) {
byte[] data = new byte[length];
System.arraycopy(bytes, offset, data, 0, length);
return encode(data);
}
}