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