网上看到的几种获取方式总结一下:
最简单的一种:String MD5 = DigestUtils.md5Hex(new FileInputStream(path));
最基本的一种:
private final static String[] strHex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; public static String getMD5One(String path) { StringBuffer sb = new StringBuffer(); try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] b = md.digest(FileUtils.readFileToByteArray(new File(path))); for (int i = 0; i < b.length; i++) { int d = b[i]; if (d < 0) { d += 256; } int d1 = d / 16; int d2 = d % 16; sb.append(strHex[d1] + strHex[d2]); } } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return sb.toString(); }
补码的形式:
public static String getMD5Two(String path) { StringBuffer sb = new StringBuffer(""); try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(FileUtils.readFileToByteArray(new File(path))); byte b[] = md.digest(); int d; for (int i = 0; i < b.length; i++) { d = b[i]; if (d < 0) { d = b[i] & 0xff; // 与上一行效果等同 // i += 256; } if (d < 16) sb.append("0"); sb.append(Integer.toHexString(d)); } } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return sb.toString(); }
这种适合大型文件
public static String getMD5Three(String path) { BigInteger bi = null; try { byte[] buffer = new byte[8192]; int len = 0; MessageDigest md = MessageDigest.getInstance("MD5"); File f = new File(path); FileInputStream fis = new FileInputStream(f); while ((len = fis.read(buffer)) != -1) { md.update(buffer, 0, len); } fis.close(); byte[] b = md.digest(); bi = new BigInteger(1, b); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bi.toString(16); }