FileChannel ch =in.getChannel();
MappedByteBuffer byteBuffer =ch.map(FileChannel.MapMode.READ_ONLY, 0,file.length());
messagedigest.update(byteBuffer);
return byteArrayToHex (messagedigest.digest());
}
我也尝试过这样的方式,但是如果文件大于2G,那么这种方式会出现异常。所以不推荐。
附2:测试文件MD5的main方法
[java]
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
try {
System.out.println(fileMD5("E:/软件/VS2008ProEdition90DayTrialCHSX1435983.iso"));
} catch (IOException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println((endTime - startTime)/1000);
}