我正在尝试匹配从服务器下载的文件的md5sum。只有在总和匹配的情况下,处理才会继续。
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
File f = new File(fileName);
InputStream is = new FileInputStream(f);
byte[] buffer = new byte[8192];
int read = 0;
while( (read = is.read(buffer)) > 0) {
digest.update(buffer, 0, read);
}
is.close();
byte[] md5sum = digest.digest();
BigInteger bigInt = new BigInteger(1, md5sum);
output = bigInt.toString(16);
System.out.println("MD5: " + output);
} catch(IOException e) {
throw new RuntimeException("Unable to process file for MD5", e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}上面的代码并不是每次都为某些文件提供正确的md5sum。
当我进入控制台并检查md5sum <filename> md5sum是否与服务器中的相同时。但是,当从代码中计算出相同时,它会产生不同的结果。
下载文件的vimdiff未提供任何diff。下载后文件正确无误。
我在上面的代码中看不到问题。
我正在尝试更改缓冲区大小。但是没有运气,所以我猜这不是因为缓冲区大小等原因。
byte[] buffer = new byte[8192];问候
Dheeraj Joshi
发布于 2012-04-23 14:43:04
BigInteger.toString方法将转义前导0,因为它们不重要。
https://stackoverflow.com/questions/10256811
复制相似问题