1. 根据文件路径获取MD5值
public static String getMd5(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;
}
}
2. Windows系统使用cmd窗口使用系统自带的MD5生成,比对java生成的是否正确
注:需要先定位到文件存放的文件夹!
语句:certutil -hashfile <filename> MD5