关于MD5校验和java工程下的校验

 1     File file = new File("cos_code2003.bin");
 2         System.out.println(file.length());
 3         byte[] data = new byte[(int) file.length()];
 4         if (file.exists()) {
 5             FileInputStream fis;
 6             try {
 7                 fis = new FileInputStream(file);
 8                 fis.read(data);
 9             } catch (FileNotFoundException e) {
10                 e.printStackTrace();
11             } catch (IOException e) {
12                 e.printStackTrace();
13             }
14         }else{
15             System.out.println("wenjian buzun");
16         }
17 
18         String a = computeMd5(data); 
19         System.out.println(a); 
20     } 
21 
22     private static String computeMd5(byte[] origin) {
23         byte[] ret = null;
24         try {
25             MessageDigest md5 = MessageDigest.getInstance("MD5");
26             ret = md5.digest(origin);
27 
28             StringBuilder hexString = new StringBuilder();
29             for (int i = 0; i < ret.length; i++) {
30                 String h = Integer.toHexString(0xFF & ret[i]);
31                 while (h.length() < 2) {
32                     h = "0" + h;
33                 }
34                 hexString.append(h);
35             }
36             return hexString.toString();
37         } catch (NoSuchAlgorithmException e) {
38             e.printStackTrace();
39         }
40 
41         return "AV";
42     }

  md5校验不难,就是知道一个MessageDigest类,获取实例,直接调用digest方法就可以了。不过校验后得到的是一个数量为16的byte数组,要转换成一个32位的16进制校验和,还需要作一下转换,注意这里用了一个StringBuilder类,把字节和0XFF进行与操作就可以了。另外注意的是当字节数少于16的时候要加一个前缀0,这里用的表示法是h.length,其它也有一些表示如0X10,其实是一样的。最后转成string就可以了。

  另外注意这里的File文件的构造函数里面的文件名的写法file,这里应该是java工程根目录下的文件名,而不是和代码在一个地方的文件名。

  上面的md5校验在计算超过2GB的大文件就会有问题,因为一个Int数组的最大只有2GB,2的31次方,否则内存会撑不住。

  这里再给出一个方法,

  

private static String checkMd5(File f) {

        FileInputStream fis = null;
        byte[] rb = null;
        DigestInputStream digestInputStream  = null;
        try {
            fis = new FileInputStream(f);
            MessageDigest md5 = MessageDigest.getInstance("md5");
            digestInputStream = new DigestInputStream(fis,
                    md5);
            byte[] buffer = new byte[4096];
            while (digestInputStream.read(buffer) > 0)
                ;
            md5 = digestInputStream.getMessageDigest();
            rb = md5.digest();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }finally{
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < rb.length; i++) {
            String a = Integer.toHexString(0XFF & rb[i]);
            if (a.length() < 2) {
                a = '0' + a;
            }
            sb.append(a);
        }
        return sb.toString();
    }

  这个方法,经测试2.7GB文件的MD5没有出错。

posted @ 2016-03-24 14:31  likeshu  阅读(2910)  评论(0编辑  收藏  举报