Merge remote-tracking branch 'origin/在线访问lhj' into 在线访问lhj

在线访问lhj
李宏杰 8 months ago
commit c74321395d

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