Help Center/ MetaStudio/ API Reference/ Appendix/ Using the Java Code to Generate the MD5 Value of the File Content
Updated on 2025-04-03 GMT+08:00

Using the Java Code to Generate the MD5 Value of the File Content

The MD5 value of the file content is the Base64-encoded 128-bit MD5 digest of the message according to RFC 1864.

The following is an example of the Java code used to generate the MD5 value of the file content:

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("File address");
        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);
    }
}