位于 FTP 服务器上的文件上的 Java 校验和 md5



>我有一个程序,需要比较位于本地磁盘上的文件和FTP服务器上的文件。我决定使用 md5 校验和。我可以使用本地文件执行此操作,但是使用ftp文件时遇到了问题。另外,我正在使用Apache FTPClient common。

MessageDigest digest = MessageDigest.getInstance("MD5");
        FileInputStream is = new FileInputStream(FTP_listFiles[i]); //ERROR HERE   
                                                                    //FTP_files is a FTPFile from FTPClient apache commons.
        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);
            System.out.println("MD5: n" + 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);
            }
        }

注意:如果不可能,你知道md5的任何等价物,但也可以做吗?

想要实现什么?您是否知道要在本地计算 md5,您需要下载完整文件?

有一个特殊的 ftp 扩展,它为客户端提供服务器端 md5,但通常不受支持。一些服务器还实现了一些类似的专有功能;您必须检查您的特定服务器。

例如,您可能想查看XCRC或XMD5或XSHA1命令。

最新更新