Java – Get MD5 Checksum of a File

The Apache Commons Codec could help you to generate the MD5 of a file with just one command. Download the lib @ Download Commons Codec. If you want to use Maven, include the following dependency in the pom.xml.

...
<dependency>
  <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  <version>1.3</version>
</dependency>
...

 

Try the following example.

package com.wordpress.ykyuen;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.codec.digest.DigestUtils;

public class GenerateMd5 {

  public static void main(String[] args) {
    FileInputStream fis = null;
    try {
      // Generate md5
      fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\ykyuen.zip");
      String md5String = DigestUtils.md5Hex(fis);
      System.out.println(md5String);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

 

Done =)

Reference: StackOverflow – using Java to get a file’s md5 checksum?

8 thoughts on “Java – Get MD5 Checksum of a File”

  1. There’s no md5Hex method with InputStream as a parameter. I can see only md5Hex(byte[] data) or md5Hex(java.lang.String data)

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.