MD5验证文件是否重复

10 篇文章 0 订阅
10 篇文章 0 订阅

 根据文件生成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;
            }
        });
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值