mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
120 lines
3.6 KiB
Java
120 lines
3.6 KiB
Java
package top.xyquan.ecomdmbbackend.utils;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
/**
|
|
* 文件哈希工具类
|
|
*/
|
|
public class FileHashUtils {
|
|
|
|
/**
|
|
* 计算文件的SHA-256哈希值
|
|
*
|
|
* @param file 文件
|
|
* @return 哈希值(十六进制字符串)
|
|
*/
|
|
public static String calculateFileHash(MultipartFile file) {
|
|
try (InputStream inputStream = file.getInputStream()) {
|
|
return calculateHash(inputStream);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException("计算文件哈希失败", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 计算文件的SHA-256哈希值
|
|
*
|
|
* @param filePath 文件路径
|
|
* @return 哈希值(十六进制字符串)
|
|
*/
|
|
public static String calculateFileHash(String filePath) {
|
|
try {
|
|
Path path = Paths.get(filePath);
|
|
if (!Files.exists(path)) {
|
|
return null;
|
|
}
|
|
try (InputStream inputStream = Files.newInputStream(path)) {
|
|
return calculateHash(inputStream);
|
|
}
|
|
} catch (IOException e) {
|
|
throw new RuntimeException("计算文件哈希失败", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 计算输入流的SHA-256哈希值
|
|
*
|
|
* @param inputStream 输入流
|
|
* @return 哈希值(十六进制字符串)
|
|
*/
|
|
private static String calculateHash(InputStream inputStream) {
|
|
try {
|
|
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
|
byte[] buffer = new byte[8192];
|
|
int bytesRead;
|
|
|
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
digest.update(buffer, 0, bytesRead);
|
|
}
|
|
|
|
byte[] hashBytes = digest.digest();
|
|
return bytesToHex(hashBytes);
|
|
} catch (NoSuchAlgorithmException | IOException e) {
|
|
throw new RuntimeException("计算哈希失败", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 将字节数组转换为十六进制字符串
|
|
*
|
|
* @param bytes 字节数组
|
|
* @return 十六进制字符串
|
|
*/
|
|
private static String bytesToHex(byte[] bytes) {
|
|
StringBuilder result = new StringBuilder();
|
|
for (byte b : bytes) {
|
|
result.append(String.format("%02x", b));
|
|
}
|
|
return result.toString();
|
|
}
|
|
|
|
/**
|
|
* 检查文件是否存在
|
|
*
|
|
* @param filePath 文件路径
|
|
* @return 文件是否存在
|
|
*/
|
|
public static boolean fileExists(String filePath) {
|
|
if (filePath == null || filePath.trim().isEmpty()) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
// 统一处理为相对于项目根目录的路径
|
|
Path path;
|
|
if (filePath.startsWith("/")) {
|
|
// 去掉开头的斜杠,转换为相对于项目根目录的路径
|
|
path = Paths.get(System.getProperty("user.dir"), filePath.substring(1));
|
|
} else if (filePath.contains(":")) {
|
|
// 绝对路径,直接使用
|
|
path = Paths.get(filePath);
|
|
} else {
|
|
// 相对路径,相对于项目根目录
|
|
path = Paths.get(System.getProperty("user.dir"), filePath);
|
|
}
|
|
|
|
return Files.exists(path) && Files.isRegularFile(path);
|
|
} catch (Exception e) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|