#hash
public String generate(File file) throws NoSuchAlgorithmException,IOException {
/*
MessageDigest msgDig = MessageDigest.getInstance("MD5");
msgDig.update(Files.readAllBytes(file.toPath()));
byte[] hash =msgDig.digest();
*/
MessageDigest msgDig = MessageDigest.getInstance("MD5");
FileChannel fc = FileChannel.open(Paths.get(file.getAbsolutePath()));
long meg = 1024*1024;
long len = fc.size();
long pos=0;
while(pos<len) {
long size = Math.min(meg, len-pos);
MappedByteBuffer mbb = fc.map(MapMode.READ_ONLY, pos, size);
msgDig.update(mbb);
pos += size;
}
byte[] hash =msgDig.digest();
return DatatypeConverter.printHexBinary(hash).toUpperCase();
}
try (InputStream is = Files.newInputStream(Paths.get(file))) {
String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(is).toUpperCase();
System.out.println(md5);
}
public static String getFileChecksum(File file) throws Exception {
MessageDigest digest = MessageDigest.getInstance("MD5");
FileInputStream fis = new FileInputStream(file);
byte[] byteArray = new byte[1024];
int bytesCount = 0;
while ((bytesCount = fis.read(byteArray)) != -1) {
digest.update(byteArray, 0, bytesCount);
}
fis.close();
byte[] bytes = digest.digest();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString().toUpperCase();
}