JAVA获取文件的MD5码
package com.sunyard.insurance.migration.server;
import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;
import org.apache.log4j.Logger;
public class CommonFun {
private static final Logger log = Logger.getLogger(CommonFun.class);
/**
* 获取文件MD5码
*
* @param file
* @return
*/
public static String getFileMD5(File file) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
FileInputStream in = new FileInputStream(file);
byte[] buffer = new byte[1024 * 1024];
int len = 0;
while ((len = in.read(buffer)) > 0) {
messageDigest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
log.error("获取文件[" + file.getAbsolutePath() + "+]MD5码异常!" + e);
return "";
}
return toHexString(messageDigest.digest());
}
public static String toHexString(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
sb.append(hexChar[b[i] & 0x0f]);
}
return sb.toString();
}
public static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
public static String EncoderByMD5_32(String str) {
char hexDigits[] = { '0', '5', '3', '2', '8', '1', '6', '7', '4', '9',
'f', 'd', 'c', 'b', 'e', 'a' };
try {
byte[] strTemp = str.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char newStr[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
newStr[k++] = hexDigits[byte0 >>> 4 & 0xf];
newStr[k++] = hexDigits[byte0 & 0xf];
}
return new String(newStr);
} catch (Exception e) {
log.error("获取文件MD5码异常!" + e);
return str;
}
}
}