PackageCom.linusiyu.common;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.math.BigInteger;Importjava.security.MessageDigest; Public classMd5util { Public Staticstring MD5 (string content) {byte[] result =New byte[]{}; Try{messagedigest MD= Messagedigest.getinstance ("MD5"); Md.update (Content.getbytes ("UTF-8")); Result=md.digest (); } Catch(Exception e) {e.printstacktrace (); } //using StringBuilder or BigInteger is OK.StringBuilder SB =NewStringBuilder (32); for(inti = 0; i < result.length; i++) { intval = result[i] & 0xff; if(Val <= 0xf) {sb.append ("0"); } sb.append (Integer.tohexstring (Val)); } returnsb.tostring (). toLowerCase (); } Public StaticString MD5 (FinalFile file)throwsException {fileinputstream fis=Newfileinputstream (file); MessageDigest MD= Messagedigest.getinstance ("MD5"); byte[] buf =New byte[1024]; intLen =-1; while(len = fis.read (buf, 0, 1024))! =-1) {md.update (buf,0, Len); } fis.close (); byte[] result =md.digest (); return NewBigInteger (1, result). toString (16). toLowerCase (); }}
Java COMPUTE MD5 Value