MD5加密之提取文件的MD5特征码

 1 public static String encodeFile(String path) {
 2 
 3         try {
 4             MessageDigest digester = MessageDigest.getInstance("MD5");
 5             
 6             FileInputStream in = new FileInputStream(path);
 7             
 8             byte[] bytes = new byte[1024];
 9             int byteCount;
10             while ((byteCount = in.read(bytes)) > 0) {
11                 digester.update(bytes, 0, byteCount);
12             }
13             byte[] digest = digester.digest();
14             
15             //用StringBuffer拼接字节数组
16             StringBuffer sb = new StringBuffer();
17             for (byte b : digest) {
18                 String str = Integer.toHexString(b & 0xff);
19                 if(str.length() == 1){
20                     str = "0" + str;
21                 }
22                 sb.append(str);
23             }
24             in.close();
25             
26             return sb.toString();
27             
28         } catch (Exception e) {
29             e.printStackTrace();
30         }
31         return null;
32     }

 

posted @ 2016-04-15 14:18  王浩宇blog  阅读(1125)  评论(0编辑  收藏  举报