码迷,mamicode.com
首页 > 其他好文 > 详细

md5算法 保证下载文件的完整性

时间:2014-09-12 19:13:54      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   文件   sp   on   c   ad   new   

/**
  * compute the the md5 of input file
  * @param input file
  * @return
  * md5 string, or null
  */
 public static String md5(File file){
  try{
   MessageDigest md = MessageDigest.getInstance("MD5");
   InputStream fileStream = new FileInputStream(file);
   byte[] buffer = new byte[1024 * 100];
   int numRead = 0;
   
   while ((numRead = fileStream.read(buffer)) > 0) {
    md.update(buffer, 0, numRead);
   }
   
   fileStream.close();
   byte[] res = md.digest();
   return byte2hex(res);
  } catch(Exception e){
   return null;
  }
 }

 

用计算后的md5值与原来已经有的值进行比对,即可知道该文件是不是完整的。

md5算法 保证下载文件的完整性

标签:io   os   ar   文件   sp   on   c   ad   new   

原文地址:http://blog.csdn.net/leehu1987/article/details/39232931

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!