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?
There’s no md5Hex method with InputStream as a parameter. I can see only md5Hex(byte[] data) or md5Hex(java.lang.String data)
LikeLike
which version of Apache Commons Codec are u using?
DigestUtils
LikeLike
which version of Apache Commons Codec supports DigestUtils?
LikeLike
In this example i was using 1.3.
LikeLike
i am using 1.6 version and it’s working
LikeLike
Thanks for verifying. =)
LikeLike
I used 1.4 it worked too. This output matches md5sum command in linux
LikeLike
Thanks for your note. =D
LikeLike