計算MD5或SHA-1,一樣的就是同一個檔案
下面的程式碼,不需要額外使用第三方元件,且支援超大檔案
//計算檔案的MD5值publicstaticStringgetFileMD5(Filefile){if(!file.isFile()){returnnull;}MessageDigestdigest=null;FileInputStreamin=null;bytebuffer[]=newbyte[8192];intlen;try{digest=MessageDigest.getInstance("MD5");in=newFileInputStream(file);while((len=in.read(buffer))!=-1){digest.update(buffer,0,len);}BigIntegerbigInt=newBigInteger(1,digest.digest());returnbigInt.toString(16);}catch(Exceptione){e.printStackTrace();returnnull;}finally{try{in.close();}catch(Exceptione){e.printStackTrace();}}}
//計算檔案的SHA-1值publicstaticStringgetFileSha1(Filefile){if(!file.isFile()){returnnull;}MessageDigestdigest=null;FileInputStreamin=null;bytebuffer[]=newbyte[8192];intlen;try{digest=MessageDigest.getInstance("SHA-1");in=newFileInputStream(file);while((len=in.read(buffer))!=-1){digest.update(buffer,0,len);}BigIntegerbigInt=newBigInteger(1,digest.digest());returnbigInt.toString(16);}catch(Exceptione){e.printStackTrace();returnnull;}finally{try{in.close();}catch(Exceptione){e.printStackTrace();}}}
計算MD5或SHA-1,一樣的就是同一個檔案
下面的程式碼,不需要額外使用第三方元件,且支援超大檔案
//計算檔案的MD5值publicstaticStringgetFileMD5(Filefile){if(!file.isFile()){returnnull;}MessageDigestdigest=null;FileInputStreamin=null;bytebuffer[]=newbyte[8192];intlen;try{digest=MessageDigest.getInstance("MD5");in=newFileInputStream(file);while((len=in.read(buffer))!=-1){digest.update(buffer,0,len);}BigIntegerbigInt=newBigInteger(1,digest.digest());returnbigInt.toString(16);}catch(Exceptione){e.printStackTrace();returnnull;}finally{try{in.close();}catch(Exceptione){e.printStackTrace();}}}
//計算檔案的SHA-1值publicstaticStringgetFileSha1(Filefile){if(!file.isFile()){returnnull;}MessageDigestdigest=null;FileInputStreamin=null;bytebuffer[]=newbyte[8192];intlen;try{digest=MessageDigest.getInstance("SHA-1");in=newFileInputStream(file);while((len=in.read(buffer))!=-1){digest.update(buffer,0,len);}BigIntegerbigInt=newBigInteger(1,digest.digest());returnbigInt.toString(16);}catch(Exceptione){e.printStackTrace();returnnull;}finally{try{in.close();}catch(Exceptione){e.printStackTrace();}}}