使用Java实现获取文件MD5值工具类

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version> <!-- 或更高版本 -->
</dependency>

public class FileMD5Utils {
    
    /**
     * 使用Java标准库的MessageDigest类获取MD5值
     * @param filePath 文件路径
     * @return 文件的md5值
     */
    public static String getMD5ByMessageDigest(String filePath) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            try (InputStream is = new FileInputStream(filePath)) {
                byte[] buffer = new byte[8192];
                int read;
                while ((read = is.read(buffer)) > 0) {
                    md.update(buffer, 0, read);
                }
            }
            byte[] md5 = md.digest();
 
            StringBuilder result = new StringBuilder();
            for (byte b : md5) {
                result.append(String.format("%02x", b));
            }
 
            return result.toString();
        } catch (NoSuchAlgorithmException | IOException e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * 使用Apache Commons Codec库获取MD5值
     * @param filePath 文件路径
     * @return 文件的md5值
     */
    public static String getMD5ByApacheCommonsCodec(String filePath) {
        try {
            return DigestUtils.md5Hex(new FileInputStream(filePath));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * 使用Java NIO获取MD5值
     * @param filePath 文件路径
     * @return 文件的md5值
     */
    public static String getMD5ByNIO(String filePath) {
        try {
            Path path = new File(filePath).toPath();
            try (InputStream is = java.nio.file.Files.newInputStream(path, StandardOpenOption.READ)) {
                MessageDigest md = MessageDigest.getInstance("MD5");
                byte[] buffer = new byte[8192];
                int read;
                while ((read = is.read(buffer)) > 0) {
                    md.update(buffer, 0, read);
                }
                byte[] md5 = md.digest();
 
                StringBuilder result = new StringBuilder();
                for (byte b : md5) {
                    result.append(String.format("%02x", b));
                }
 
                return result.toString();
            }
        } catch (NoSuchAlgorithmException | IOException e) {
            e.printStackTrace();
            return null;
        }
    }
 
    public static void main(String[] args) {
        String filePath = "文件";
 
        String md5ByMessageDigest = getMD5ByMessageDigest(filePath);
        System.out.println("MD5 (java MessageDigest 标准库实现): " + md5ByMessageDigest);
 
        String md5ByApacheCommonsCodec = getMD5ByApacheCommonsCodec(filePath);
        System.out.println("MD5 (Apache Commons Codec 库实现): " + md5ByApacheCommonsCodec);
 
        String md5ByNIO = getMD5ByNIO(filePath);
        System.out.println("MD5 (Java NIO 实现): " + md5ByNIO);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值