package com.fuyuanshen.web.util; import org.apache.commons.codec.digest.DigestUtils; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; import java.util.HexFormat; /** * 文件哈希工具类 */ public class FileHashUtil { private static final String ALGORITHM = "SHA-256"; public static String hash(MultipartFile file) throws IOException { MessageDigest digest = DigestUtils.getDigest(ALGORITHM); try (InputStream in = file.getInputStream()) { byte[] buf = new byte[8192]; int len; while ((len = in.read(buf)) != -1) { digest.update(buf, 0, len); } } return HexFormat.of().formatHex(digest.digest()); } }