根据文件生成MD5标识串,当两个标识串一致时认为文件内容相同。
文件生成MD5 String方法如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.AccessController;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivilegedAction;
/**
* MD5验证文件是否为同文件
*
*/
public class MD5File {
protected static char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* 根据文件生成MD5Str
*
*/
public static String getFileMD5Str(File file) throws IOException {
String str;
FileInputStream in = new FileInputStream(file);
FileChannel ch = in.getChannel();
//700000000 bytes are about 670M
int maxSize = 700000000;
long startPosition = 0L;
long step = file.length() / maxSize;
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException nsaex) {
nsaex.printStackTrace();
in.close();
ch.close();
throw new JPMException("初始化失败,MessageDigest不支持MD5!");
}
if (step == 0) {
MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
messageDigest.update(byteBuffer);
str = bufferToHex(messageDigest.digest());
closeAll(in,ch,byteBuffer);
return str;
}
for (int i = 0; i < step; i++) {
MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, startPosition, maxSize);
messageDigest.update(byteBuffer);
startPosition += maxSize;
bufferUnmap(byteBuffer);
}
if (startPosition == file.length()) {
str = bufferToHex(messageDigest.digest());
ch.close();
in.close();
return str;
}
MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, startPosition, file.length() - startPosition);
messageDigest.update(byteBuffer);
str = bufferToHex(messageDigest.digest());
closeAll(in,ch,byteBuffer);
return str;
}
private static String bufferToHex(byte bytes[]) {
return bufferToHex(bytes, 0, bytes.length);
}
private static String bufferToHex(byte bytes[], int m, int n) {
StringBuffer stringbuffer = new StringBuffer(2 * n);
int k = m + n;
for (int l = m; l < k; l++) {
appendHexPair(bytes[l], stringbuffer);
}
return stringbuffer.toString();
}
private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
char c0 = hexDigits[(bt & 0xf0) >> 4];
char c1 = hexDigits[bt & 0xf];
stringbuffer.append(c0);
stringbuffer.append(c1);
}
private static void closeAll(FileInputStream in,FileChannel ch ,MappedByteBuffer buffer) throws IOException{
bufferUnmap(buffer);
ch.close();
in.close();
}
/**
* 释放MappedByteBuffer
*
* @param buffer
*/
private static void bufferUnmap(MappedByteBuffer buffer) {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
Method getCleanerMethod = buffer.getClass().getMethod("cleaner", new Class[0]);
getCleanerMethod.setAccessible(true);
sun.misc.Cleaner cleaner = (sun.misc.Cleaner)
getCleanerMethod.invoke(buffer, new Object[0]);
cleaner.clean();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
});
}
}