在 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屋!
查看全文