java计算过G文件md5 值计算

 

  1 package io.bigdata;

  2 

  3 import java.io.File;

  4 import java.io.FileInputStream;

  5 import java.io.IOException;

  6 import java.io.InputStream;

  7 import java.security.MessageDigest;

  8 import java.security.NoSuchAlgorithmException;

  9 import java.security.Provider;

 10 import java.security.Security;

 11 import java.util.HashSet;

 12 import java.util.Iterator;

 13 import java.util.Set;

 14 

 15 import org.apache.commons.codec.digest.DigestUtils;

 16 

 17 public class Md5CaculateUtil {

 18 

 19     private Md5CaculateUtil(){

 20         

 21     }

 22     

 23     private static char[] hexChar = {

 24         '0','1','2','3','4','5','6','7','8','9',

 25         'a','b','c','d','e','f'

 26     };

 27     

 28     public static String getHash(String fileName,String hashType) throws IOException, NoSuchAlgorithmException{

 29         

 30         File f = new File(fileName);

 31         System.out.println(" -------------------------------------------------------------------------------");

 32         System.out.println("|当前文件名称:"+f.getName());

 33         System.out.println("|当前文件大小:"+(f.length()/1024/1024)+"MB");

 34         System.out.println("|当前文件路径[绝对]:"+f.getAbsolutePath());

 35         System.out.println("|当前文件路径[---]:"+f.getCanonicalPath());

 36         System.out.println(" -------------------------------------------------------------------------------");

 37         

 38         InputStream ins = new FileInputStream(f);

 39         

 40         byte[] buffer = new byte[8192];

 41         MessageDigest md5 = MessageDigest.getInstance(hashType);

 42         

 43         int len;

 44         while((len = ins.read(buffer)) != -1){

 45             md5.update(buffer, 0, len);

 46         }

 47 

 48         ins.close();

 49 //        也可以用apache自带的计算MD5方法

 50         return DigestUtils.md5Hex(md5.digest());

 51 //        自己写的转计算MD5方法

 52 //        return toHexString(md5.digest());

 53     }

 54     

 55     public static String getHash2(String fileName){

 56         File f = new File(fileName);

 57         return String.valueOf(f.lastModified());

 58     }

 59     

 60     

 61     protected static String toHexString(byte[] b){

 62         StringBuilder sb = new StringBuilder(b.length*2);

 63         for(int i=0;i<b.length;i++){

 64             sb.append(hexChar[(b[i] & 0xf0) >>> 4]);

 65             sb.append(hexChar[b[i] & 0x0f]);

 66         }

 67         return sb.toString();

 68     }

 69     

 70     /*

 71      * 获取MessageDigest支持几种加密算法

 72      */

 73     @SuppressWarnings({ "rawtypes", "unchecked" })

 74     private static String[] getCryptolmpls(String serviceType){

 75         

 76         Set result = new HashSet();

 77 //        all providers

 78         Provider[] providers = Security.getProviders();

 79         for(int i=0;i<providers.length;i++){

 80 //            get services provided by each provider

 81             Set keys = providers[i].keySet();

 82             for(Iterator it = keys.iterator();it.hasNext();){

 83                 String key = it.next().toString();

 84                 key = key.split(" ")[0];

 85                 

 86                 if(key.startsWith(serviceType+".")){

 87                     result.add(key.substring(serviceType.length()+1));

 88                 }else if(key.startsWith("Alg.Alias."+serviceType+".")){

 89                     result.add(key.substring(serviceType.length()+11));

 90                 }

 91             }

 92         }

 93         return (String[]) result.toArray(new String[result.size()]);

 94     }

 95     

 96     

 97     public static void main(String[] args) throws Exception, Exception {

 98 //        调用方法

 99 //        String[] names = getCryptolmpls("MessageDigest");

100 //        for(String name:names){

101 //            System.out.println(name);

102 //        }

103         long start = System.currentTimeMillis();

104         System.out.println("开始计算文件MD5值,请稍后...");

105         String fileName = "E:\\Office_2010_Toolkit_2.2.3_XiaZaiBa.zip";

106 ////        String fileName = "E:\\SoTowerStudio-3.1.0.exe";

107         String hashType = "MD5";

108         String hash = getHash(fileName,hashType);

109         System.out.println("MD5:"+hash);

110         long end = System.currentTimeMillis();

111         System.out.println("一共耗时:"+(end-start)+"毫秒");

112     }

113 }

 

开始计算文件MD5值,请稍后...
-------------------------------------------------------------------------------
|当前文件名称:Office_2010_Toolkit_2.2.3_XiaZaiBa.zip
|当前文件大小:2437MB
|当前文件路径[绝对]:E:\Office_2010_Toolkit_2.2.3_XiaZaiBa.zip
|当前文件路径[---]:E:\Office_2010_Toolkit_2.2.3_XiaZaiBa.zip
-------------------------------------------------------------------------------
MD5:b1d269391c87942e5bb32b575baa0d79
一共耗时:18794毫秒

 参考文章:http://sziitjiang.iteye.com/blog/1702961

你可能感兴趣的:(java)