转载自:http://blog.youkuaiyun.com/xiao__gui/article/details/8148203
对文件进行MD5也可以像字符串MD5一样的,首先要把文件转成字节数组,后面和字符串MD5完全一样。
但是如果是一个特别大的文件,一下子把一个文件的数组全部读到内存中,那么估计内存也吃不消。
对于大文件,可以使用DigestInputStream。
-
- 获取文件的MD5值
-
- public static String fileMD5(String inputFile) throws IOException {
- // 缓冲区大小(这个可以抽出一个参数)
- int bufferSize = 256 * 1024;
- FileInputStream fileInputStream = null;
- DigestInputStream digestInputStream = null;
- try {
- // 拿到一个MD5转换器(同样,这里可以换成SHA1)
- MessageDigest messageDigest =MessageDigest.getInstance("MD5");
- // 使用DigestInputStream
- fileInputStream = new FileInputStream(inputFile);
- digestInputStream = new DigestInputStream(fileInputStream,messageDigest);
- // read的过程中进行MD5处理,直到读完文件
- byte[] buffer =new byte[bufferSize];
- while (digestInputStream.read(buffer) > 0);
- // 获取最终的MessageDigest
- messageDigest= digestInputStream.getMessageDigest();
- // 拿到结果,也是字节数组,包含16个元素
- byte[] resultByteArray = messageDigest.digest();
- // 同样,把字节数组转换成字符串
- return byteArrayToHex(resultByteArray);
- } catch (NoSuchAlgorithmException e) {
- return null;
- } finally {
- try {
- digestInputStream.close();
- } catch (Exception e) {
- }
- try {
- fileInputStream.close();
- } catch (Exception e) {
- }
- }
- }
- public static String byteArrayToHex(byte[] byteArray) {
- // 首先初始化一个字符数组,用来存放每个16进制字符
- char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' };
- // new一个字符数组,这个就是用来组成结果字符串的(解释一下:一个byte是八位二进制,也就是2位十六进制字符(2的8次方等于16的2次方))
- char[] resultCharArray =new char[byteArray.length * 2];
- // 遍历字节数组,通过位运算(位运算效率高),转换成字符放到字符数组中去
- int index = 0;
- for (byte b : byteArray) {
- resultCharArray[index++] = hexDigits[b>>> 4 & 0xf];
- resultCharArray[index++] = hexDigits[b& 0xf];
- }
- // 字符数组组合成字符串返回
- return new String(resultCharArray);