如何使用Java计算修改文件的MD5值
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;
public class TestMD5 {
public static void main(String[] args){
for (String filepath : args) {
String md5 = computeMD5(new File(filepath));
System.out.println(md5);
}
}
private static String computeMD5(File file) {
DigestInputStream din = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
//第一个参数是一个输入流
din = new DigestInputStream(new BufferedInputStream(new FileInputStream(file)), md5);
byte[] b = new byte[1024];
while (din.read(b) != -1);
byte[] digest = md5.digest();
StringBuilder result = new StringBuilder(file.getName());
result.append(": ");
result.append(DatatypeConverter.printHexBinary(digest));
return result.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (din != null) {
din.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}