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

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

@ -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();
if (fileInputStream!= null) { // 如果文件输入流不为空
fileInputStream.close(); // 关闭文件输入流
}
} catch (IOException e) {
e.printStackTrace();
} catch (IOException e) { // 捕获输入输出异常
e.printStackTrace(); // 打印异常信息
}
}
}
}
Loading…
Cancel
Save