用Java获取文件的MD5校验和 [英] Getting a File's MD5 Checksum in Java
本文介绍了用Java获取文件的MD5校验和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望使用Java来获取文件的MD5校验和。我真的很惊讶,但我找不到任何显示如何获取文件的MD5校验和的内容。
I am looking to use Java to get the MD5 checksum of a file. I was really surprised but I haven't been able to find anything that shows how to get the MD5 checksum of a file.
如何完成?
推荐答案
有一个输入流装饰器, java.security.DigestInputStream
,这样你就可以像往常一样使用输入流计算摘要,而不是必须对数据进行额外的传递。
There's an input stream decorator, java.security.DigestInputStream
, so that you can compute the digest while using the input stream as you normally would, instead of having to make an extra pass over the data.
MessageDigest md = MessageDigest.getInstance("MD5");
try (InputStream is = Files.newInputStream(Paths.get("file.txt"));
DigestInputStream dis = new DigestInputStream(is, md))
{
/* Read decorated stream (dis) to EOF as normal... */
}
byte[] digest = md.digest();
这篇关于用Java获取文件的MD5校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文