【发布时间】: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,你需要下载完整的文件吗?
-
您遇到了什么问题?用什么代码?