【问题标题】:Java checksum md5 on a file situated on a FTP server位于 FTP 服务器上的文件上的 Java 校验和 md5
【发布时间】:2014-03-02 19:08:50
【问题描述】:

我有一个程序需要比较一个位于本地磁盘上的文件和一个位于 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,你需要下载完整的文件吗?
  • 您遇到了什么问题?用什么代码?

标签: java file ftp md5


【解决方案1】:

想要达到什么目的?你知道要在本地计算 md5,你需要下载完整的文件吗?

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

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

【讨论】:

  • 那么,除了使用 md5 之外,您还有其他解决方案吗?但效果大致相同?
  • 不,因为.this 要成为任何您的服务器必须支持校验和命令之一,请参阅我的更新答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-24
相关资源
最近更新 更多