有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java文件md5哈希在分块时发生变化(用于netty传输)

Question at the bottom

我正在使用netty将文件传输到另一台服务器。 由于WebSocket协议,我将文件块限制为1024*64字节(64KB)。以下方法是该文件将发生的本地示例:

public static void rechunck(File file1, File file2) {

    FileInputStream is = null;
    FileOutputStream os = null;

    try {

        byte[] buf = new byte[1024*64];

        is = new FileInputStream(file1);
        os = new FileOutputStream(file2);

        while(is.read(buf) > 0) {
            os.write(buf);
        }

    } catch (IOException e) {
        Controller.handleException(Thread.currentThread(), e);
    } finally {

        try {

            if(is != null && os != null) {
                is.close();
                os.close();
            }

        } catch (IOException e) {
            Controller.handleException(Thread.currentThread(), e);
        }

    }

}

文件由InputStream加载到字节缓冲区,并直接写入OutputStream。 在此过程中,文件内容无法更改

为了获得文件的md5-hashes,我编写了以下方法:

public static String checksum(File file) {

    InputStream is = null;

    try {

        is = new FileInputStream(file);
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] buffer = new byte[8192];
        int read = 0;

        while((read = is.read(buffer)) > 0) {
            digest.update(buffer, 0, read);
        }

        return new BigInteger(1, digest.digest()).toString(16);

    } catch(IOException | NoSuchAlgorithmException e) {
        Controller.handleException(Thread.currentThread(), e);
    } finally {

        try {
            is.close();
        } catch(IOException e) {
            Controller.handleException(Thread.currentThread(), e);
        }

    }

    return null;

}

所以:理论上它应该返回相同的散列,不是吗?问题是,它返回两个不同的哈希值,但每次运行都不不同。。文件大小保持不变,内容也保持不变。 当我为in: file-1out: file-2运行一次该方法,并再次使用in: file-2out: file-3运行该方法时,file-2和file-3的哈希值是相同的!这意味着该方法每次都会以相同的方式正确更改文件

1. 58a4a9fbe349a9e0af172f9cf3e6050a
2. 7b3f343fa1b8c4e1160add4c48322373
3. 7b3f343fa1b8c4e1160add4c48322373

这里有一个小测试,比较所有缓冲区是否相等。检测呈阳性。所以没有任何区别

File file1 = new File("controller/templates/Example.zip");
File file2 = new File("controller/templates2/Example.zip");

try {

    byte[] buf1 = new byte[1024*64];
    byte[] buf2 = new byte[1024*64];

    FileInputStream is1 = new FileInputStream(file1);
    FileInputStream is2 = new FileInputStream(file2);

    boolean run = true;
    while(run) {

        int read1 = is1.read(buf1), read2 = is2.read(buf2);
        String result1 = Arrays.toString(buf1), result2 = Arrays.toString(buf2);
        boolean test = result1.equals(result2);

        System.out.println("1: " + result1);
        System.out.println("2: " + result2);
        System.out.println("--- TEST RESULT: " + test + " ----------------------------------------------------");

        if(!(read1 > 0 && read2 > 0) || !test) run = false;

    }

} catch (IOException e) {
    e.printStackTrace();
}

问题:你能帮我在不改变散列的情况下对文件进行分块吗


共 (1) 个答案

  1. # 1 楼答案

    你的rechunk方法有一个漏洞。因为你有一个固定的缓冲区,你的文件被分割成字节数组部分。但是文件的最后一部分可能比缓冲区小,这就是为什么在新文件中写入太多字节的原因。这就是为什么不再有相同的校验和。错误可以这样修复:

    public static void rechunck(File file1, File file2) {
    
        FileInputStream is = null;
        FileOutputStream os = null;
    
        try {
    
            byte[] buf = new byte[1024*64];
    
            is = new FileInputStream(file1);
            os = new FileOutputStream(file2);
            int length;
            while((length = is.read(buf)) > 0) {
                os.write(buf, 0, length);
            }
    
        } catch (IOException e) {
            Controller.handleException(Thread.currentThread(), e);
        } finally {
    
            try {
    
                if(is != null)
                    is.close();
                if(os != null)
                    os.close();
    
            } catch (IOException e) {
                Controller.handleException(Thread.currentThread(), e);
            }
    
        }
    
    }
    

    由于长度变量,write方法知道,在字节数组的字节x关闭之前,只有文件处于关闭状态,那么其中仍然有不再属于该文件的旧字节