Java compute file 32-bit MD5 value

Source: Internet
Author: User

1 protected Staticstring getFileMD5 (string fileName)2     {3File File =NewFile (fileName);4         if(!file.exists () | | |File.isfile ()) {5             return"";6         }7         8         byte[] buffer =New byte[2048];9         Try {TenMessageDigest digest = messagedigest.getinstance ("MD5"); OneFileInputStream in =Newfileinputstream (file); A              while(true){ -                 intLen = In.read (buffer,0,2048); -                 if(Len! =-1){ theDigest.update (buffer, 0, Len); -}Else{ -                      Break; -                 } +             } - in.close (); +              A             byte[] Md5bytes =digest.digest (); atStringBuffer Hexvalue =NewStringBuffer (); -              for(inti = 0; i < md5bytes.length; i++) { -                 intval = ((int) Md5bytes[i]) & 0xFF; -                 if(Val < 16) { -Hexvalue.append ("0"); -                 } in Hexvalue.append (Integer.tohexstring (Val)); -             } to             returnhexvalue.tostring (); +              -             //String hash = new BigInteger (1,digest.digest ()). ToString (+); the             //return hash; *              $}Catch(nosuchalgorithmexception e) {Panax Notoginseng             //TODO auto-generated Catch block - e.printstacktrace (); the             return""; +}Catch(FileNotFoundException e) { A             //TODO auto-generated Catch block the e.printstacktrace (); +             return""; -}Catch(IOException e) { $             //TODO auto-generated Catch block $ e.printstacktrace (); -             return""; -         } the}

How to use:

1 String file = "D://one.zip"; 2 String MD5 = getFileMD5 (file);

Java compute file 32-bit MD5 value

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.