MD5对文件生成16进制字符串,由于文件较大,需要通过读取方式一段一段的update,那么就与这个byte[]数组的大小有关,也就是下面方法中的CACHE_SIZE,我设置1024生成的MD5与设置2048生成的MD5是不同的,那么这种问题怎么处理?还是有个不成文的默认尺寸,比如1M,大家都用这么大的缓存来验证?请知道的朋友指点迷津,谢谢!
public static String createFileMD5(String filePath) throws Exception {
String md5 = "";
File file = new File(filePath);
if (file.exists()) {
MessageDigest messageDigest = getMD5();
FileInputStream in = new FileInputStream(file);
byte[] cache = new byte[CACHE_SIZE];
while (in.read(cache) != -1) {
messageDigest.update(cache);
}
in.close();
byte data[] = messageDigest.digest();
md5 = byteArrayToHexString(data);
}
return md5;
}