问题:使用BigInteger 会出现首位为0时MD5值出现31位的现象。
获取单个文件MD5 其中一种操作方式
/**
* 获取单个文件的MD5值!
* @param file
* @return
*/
public static String getFileMD5(File file) {
if (!file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(16);
}
获取单个文件MD5 另一种操作方式
public String getMd5(File file){
String value = null;
FileInputStream in = null;
try {
in = new FileInputStream(file);
MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(byteBuffer);
BigInteger bi = new BigInteger(1, md5.digest());
value = bi.toString(16);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return value;
}
上面的两种方法都是使用了BigInteger 来保存后,再转化成字符串,都会出现首位为0被忽略的现象。
下面的方法可以解决上述的问题
/**
* 获取单个文件的MD5值!
* @param file
* @return
* 解决首位0被省略问题
*/
public static String getFileMD5(File file) {
StringBuffer stringbuffer = null;
try {
char[] hexDigits = { '0', '1', '2','3', '4','5', '6','7','8', '9', 'a','b' ,'c', 'd','e', 'f' };
FileInputStream in = new FileInputStream(file);
FileChannel ch = in.getChannel();
MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,file.length());
MessageDigest messagedigest = MessageDigest.getInstance("MD5");
messagedigest.update(byteBuffer);
byte[] bytes = messagedigest.digest();
int n = bytes.length;
stringbuffer = new StringBuffer(2 * n);
for (int l = 0; l < n; l++) {
byte bt = bytes[l];
char c0 = hexDigits[(bt & 0xf0) >> 4];
char c1 = hexDigits[bt & 0xf];
stringbuffer.append(c0);
stringbuffer.append(c1);
}
} catch (Exception e) {
e.printStackTrace();
}
return stringbuffer.toString();
}
但是上面这种方式,如果文件超过2G,会超过 FileChannel 的 map 方法中 size 参数大小限制,源码中发现该参数值大于 Integer.MAX_VALUE 时会直接抛出 IllegalArgumentException(“Size exceeds Integer.MAX_VALUE”) 异常,所以对于特别大的文件其依然不适合。
最终写法参照这篇文章: