import java.security.MessageDigest;
//获取文件md5值
public static String md5HashCode(String filePath) {
try {
if (!new File(filePath).isFile()) {
System.err.println("Error: " + filePath
+ " is not a valid file.");
return null;
}
InputStream fis = new FileInputStream(filePath);
byte[] buffer = new byte[1024];
MessageDigest complete = MessageDigest.getInstance("MD5");
int numRead = -1;
while ((numRead = fis.read(buffer)) != -1) {
complete.update(buffer, 0, numRead);
}
byte[] b = complete.digest();
if (null == b) {
System.err.println(("Error:create md5 string failure!"));
return null;
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < b.length; i++) {
result.append(Integer.toString((b[i] & 0xff) + 0x100, 16)
.substring(1));
}
return result.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35