1

我正在尝试为文件夹中的某些文件开发文件更新程序,以将 FTP 服务器与本地文件夹同步,在客户端使用 Java,在服务器端使用 PHP。

在服务器端,我正在计算md5_file($filename)文件并以 JSON 格式返回它们中的每一个。

在 Java 上,我首先检查文件是否存在于本地文件夹中。如果该文件存在,那么我检查 MD5 校验和以查看该文件是否与在线文件完全相同。

检查 .txt 或 .lua 文件时,MD5 不匹配。检查其他文件类型时可以,例如 .dds 纹理文件。

我在 Java 上使用的 MD5 是这样的:

private String md5(File f) throws FileNotFoundException, NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance("MD5");
    InputStream is = new FileInputStream(f);                
    byte[] buffer = new byte[8192];
    int read = 0;
    try {
        while( (read = is.read(buffer)) > 0) {
            digest.update(buffer, 0, read);
        }       
        byte[] md5sum = digest.digest();
        BigInteger bigInt = new BigInteger(1, md5sum);
        String output = bigInt.toString(16);
        return output;
    }
    catch(IOException e) {
        throw new RuntimeException("Unable to process file for MD5", e);
    }
    finally {
        try {
            is.close();
        }
        catch(IOException e) {
            throw new RuntimeException("Unable to close input stream for MD5 calculation", e);
        }
    }
}

例如,对于一个 description.lua 文件,其内容如下:

livery = {
{"KC-130_fusel", 0 ,"KC-130_map_fus",false};
{"KC-130_wing", 0 ,"KC-130_map_wingS",false};
{"KC-130_wing_2", 0 ,"KC-130_map_wings_2",false};
{"KC-130_notes", 0 ,"KC-130_notes_empty",true};
{"KC-130_FPod", 0 ,"kc-130_map_drg",false};
}
name = "Spain ALA 31 TK.10-06"
countries = {"SPN"} -- and any others you want to add

PHP md5_file($filename) = d0c32f9e38cc6e1bb8b54a6aca4a0190

JAVA md5(文件)= 08bf57441b904c69e9ce3ca02a9257c7

我一直试图找到这两个代码之间的关系,看看有什么不同,但还没有找到。我检查了 10 个用于 Java 的 md5 脚本,它们都给出了相同的结果。

有什么办法可以解决这个问题吗?

编辑:第一条评论给出的解决方案:将 FTP 客户端上的传输类型更改为二进制,以避免将 txt 文件更改为 ASCII 编码,更改它们的长度和 md5。

4

0 回答 0