[Java] uses MD5 to determine whether two files are identical

Source: Internet
Author: User

1. Preface

Because comparing two pictures is the same, if you feel slower by Pixel point comparison, the efficiency is much lower when many images are compared. Since each file MD5 is essentially unique, it is used to determine whether the file is the same as the MD5 to get the file.

2. The code is as follows:

Import Java.io.fileinputstream;import Java.io.ioexception;import org.apache.commons.codec.digest.DigestUtils;  Import Org.apache.commons.io.ioutils;public class TestMd5 {public static void main (string[] args) throws IOException {// TODO auto-generated method stubstring path= "C:\\philips\\siserver\\img\\p1.jpg"; String path2= "C:\\philips\\siserver\\img\\p2.jpg";                 FileInputStream fis= New FileInputStream (path);            String MD5 = Digestutils.md5hex (Ioutils.tobytearray (FIS));            ioutils.closequietly (FIS);            System.out.println ("P1_MD5:" +md5);                 FileInputStream fis2= New FileInputStream (path2);            String md52 = Digestutils.md5hex (Ioutils.tobytearray (FIS2));            ioutils.closequietly (FIS2);            System.out.println ("P2_MD5:" +md52);}}

The log after execution is as follows:

P1_md5:699418360ea96b76c0d4cb701decd2e7
P2_md5:699418360ea96b76c0d4cb701decd2e7

3. Picture Resources

Link:p1.jpg p2.jpg is just rename p1.jpg.

[Java] uses MD5 to determine whether two files are identical

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.