import java.security.DigestInputStream;
import java.security.MessageDigest;
/**
*
* @Description MD5校验
* @param file 要校验的文件
* @param md5String 正确的MD5参考
* @return
* @throws IOException
*
*/
private boolean md5Check(File file,String md5String) throws IOException{
MessageDigest md5Digest = null;
InputStream is = null;
try {
md5Digest = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[10240]; //根据文件大小调整缓冲区大小
is = new DigestInputStream(new FileInputStream(file), md5Digest);
while (is.read(buffer) > 0) {
}
}catch(Exception e){
throw new IOException("校验MD5出错", e);
}finally {
try {
is.close();
} catch (Exception e) {
}
}
byte[] digest = md5Digest.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < digest.length; i++) {
String hex = Integer.toHexString(0xff & digest[i]);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString().equalsIgnoreCase(md5String);
}