为了图方便,使用网上的一个方法获取MD5值,有时候会出错,才发现是它有时候是31位,没有补齐32位,其实就是首位补0.
public static String getFileMD5(File file) {
if (!file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
BigInteger bigInt = new BigInteger(1, digest.digest());
String ret = bigInt.toString(16);
//!!! 注意补0!!!!
while (ret.length() < 32) {
ret = "0" + ret;
}
return ret;
}
评论区