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?

About these ads

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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s