/**
* 获取文件md5值
*/
private String md5HashCode(String filePath) {
try {
InputStream fis = new FileInputStream(filePath);
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[1024];
int length = -1;
while ((length = fis.read(buffer, 0, 1024)) != -1) {
md.update(buffer, 0, length);
}
fis.close();
//转换并返回包含16个元素字节数组,返回数值范围为-128到127
byte[] md5Bytes = md.digest();
BigInteger bigInt = new BigInteger(1, md5Bytes);
return bigInt.toString(16);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
java获取文件MD5值
最新推荐文章于 2024-01-16 18:31:25 发布
该代码段展示了如何使用Java获取文件的MD5哈希值。通过实例化MessageDigest,读取文件并更新摘要,最后将结果转换为16进制字符串。此方法适用于文件校验或比较。
4043

被折叠的 条评论
为什么被折叠?



