diff --git a/tamguo-mms/src/main/java/com/tamguo/utils/FileMd5Utils.java b/tamguo-mms/src/main/java/com/tamguo/utils/FileMd5Utils.java index 539a8ba..f109114 100644 --- a/tamguo-mms/src/main/java/com/tamguo/utils/FileMd5Utils.java +++ b/tamguo-mms/src/main/java/com/tamguo/utils/FileMd5Utils.java @@ -1,37 +1,42 @@ package com.tamguo.utils; -import java.io.FileInputStream; -import java.io.IOException; -import java.security.MessageDigest; -import org.apache.commons.codec.binary.Hex; +import java.io.FileInputStream; // 导入文件输入流类 +import java.io.IOException; // 导入输入输出异常类 +import java.security.MessageDigest; // 导入消息摘要类 +import org.apache.commons.codec.binary.Hex; // 导入十六进制编码类 +/** + * FileMd5Utils 类,用于获取文件的 MD5 值(可处理大文件) + */ public class FileMd5Utils { /** - * 获取一个文件的md5值(可处理大文件) - * @return md5 value + * 获取一个文件的 MD5 值的方法 + * @param fileInputStream 文件输入流 + * @return MD5 值字符串 */ public static String getMD5(FileInputStream fileInputStream) { try { + // 获取 MD5 消息摘要实例 MessageDigest MD5 = MessageDigest.getInstance("MD5"); + // 创建字节数组,大小为 8192 字节 byte[] buffer = new byte[8192]; - int length; - while ((length = fileInputStream.read(buffer)) != -1) { - MD5.update(buffer, 0, length); + int length; // 定义读取到的字节长度变量 + while ((length = fileInputStream.read(buffer))!= -1) { // 循环读取文件内容 + MD5.update(buffer, 0, length); // 更新消息摘要 } - return new String(Hex.encodeHex(MD5.digest())); - } catch (Exception e) { - e.printStackTrace(); - return null; - } finally { + return new String(Hex.encodeHex(MD5.digest())); // 将消息摘要的结果进行十六进制编码并转换为字符串返回 + } catch (Exception e) { // 捕获异常 + e.printStackTrace(); // 打印异常信息 + return null; // 返回空值 + } finally { // 无论是否发生异常,都会执行以下代码块 try { - if (fileInputStream != null){ - fileInputStream.close(); - } - } catch (IOException e) { - e.printStackTrace(); + if (fileInputStream!= null) { // 如果文件输入流不为空 + fileInputStream.close(); // 关闭文件输入流 + } + } catch (IOException e) { // 捕获输入输出异常 + e.printStackTrace(); // 打印异常信息 } } } - -} +} \ No newline at end of file