项目需要,计算文件的MD5和SHA1值,找了一些代码效率比较低,有的还晦涩难懂,这里给出测试后通过,速度也相对较快的代码。
/**
* Get the md5 value of the filepath specified file
* @param filePath The filepath of the file
* @return The md5 value
*/
public String fileToMD5(String filePath) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(filePath); // Create an FileInputStream instance according to the filepath
byte[] buffer = new byte[1024]; // The buffer to read the file
MessageDigest digest = MessageDigest.getInstance("MD5"); // Get a MD5 instance
int numRead = 0; // Record how many bytes have been read
w