Import Java. io. fileinputstream; import Java. io. ioexception; import Java. security. digestinputstream; import Java. security. messagedigest; import Java. security. nosuchalgorithmexception; public class filemd5 {public static void main (string [] ARGs) {long starttime = system. currenttimemillis (); try {system. out. println (filemd5 ("F: \ tddownload \ 123.rar"); // bdeae194a93c186b7b42a7a6425b6310} catch (ioexception e) {e. printstacktrace ();} Long endtime = system. currenttimemillis (); system. out. println (endtime-starttime)/1000);} public static string filemd5 (string inputfile) throws ioexception {// buffer size (this can extract a parameter) int buffersize = 256*1024; fileinputstream = NULL; digestinputstream = NULL; try {// get an MD5 converter (likewise, you can change it to sha1) messagedigest = messagedigest. getinstance ("MD5"); // use digestinputstreamfileinputstream = new fileinputstream (inputfile); digestinputstream = new digestinputstream (fileinputstream, messagedigest); // perform MD5 processing during read, until the byte [] buffer = new byte [buffersize]; while (digestinputstream. read (buffer)> 0); // obtain the final messagedigestmessagedigest = digestinputstream. getmessagedigest (); // get the result, which is also a byte array containing 16 elements, byte [] resultbytearray = messagedigest. digest (); // Similarly, convert the byte array to the string return bytearraytohex (resultbytearray);} catch (nosuchalgorithmexception e) {return NULL;} finally {try {digestinputstream. close ();} catch (exception e) {e. printstacktrace ();} Try {fileinputstream. close ();} catch (exception e) {e. printstacktrace () ;}}// the following function is used to convert the byte array into a hexadecimal string public static string bytearraytohex (byte [] bytearray) {// Initialize an array of characters to store each hexadecimal char [] hexdigits = {'0', '1', '2', '3 ', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D ', 'E', 'F'}; // a new character array, which is used to form the result string (to explain: A byte is an eight-bit binary, that is, two hexadecimal characters (the power of 2 is equal to the power of 16) Char [] resultchararray = new char [bytearray. length * 2]; // traverses the byte array. bitwise operations are performed (bit operation efficiency is high) to convert it into characters and place them in the character array. Int Index = 0; For (byte B: bytearray) {resultchararray [index ++] = hexdigits [B >>> 4 & 0xf]; resultchararray [index ++] = hexdigits [B & 0xf];} // returns return new string (resultchararray) when combining character arrays into strings );}}