更新时间:2024-11-07 GMT+08:00
使用Java代码生成文件内容的MD5值
生成文件内容的MD5值对应的Java代码示例,如下所示:
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Base64; public class DacBase64Main { public static void main(String[] args) throws NoSuchAlgorithmException, IOException { MessageDigest messageDigest = MessageDigest.getInstance("MD5"); FileInputStream fileInputStream = new FileInputStream("文件地址"); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); final byte[] buffer = new byte[1024]; int read; while ((read = bufferedInputStream.read(buffer, 0, 1024)) > -1) { messageDigest.update(buffer, 0, read); } byte[] digest = messageDigest.digest(); String md5 = Base64.getEncoder().encodeToString(digest); System.out.println(md5); } }