java获取文件md5码
2016-03-17 16:14
417 查看
最近下载了一些文件,不是官方的,为了核对MD5码写了一个java版本的,小工具。
百度网盘:http://pan.baidu.com/s/1bgUFHc
package pri.yang.MD5; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache.commons.codec.binary.Hex; public class BigFileMD5 { static MessageDigest MD5 = null; static{ try{ MD5 = MessageDigest.getInstance("MD5"); }catch(NoSuchAlgorithmException e){ e.printStackTrace(); } } public static void main(String[] args){ if(args.length < 1){ System.out.println("need one argument as file path!"); }else{ String filePath = args[0]; //File file = new File("F:\\BaiduYunDownload\\jdk-7u79-linux-x64.tar.gz"); File file = new File(filePath); if(!file.exists()){ System.out.println("incorrect file path!"); }else{ String md5 = getMD5(file); System.out.println("===============MD5==============="); System.out.println(md5); System.out.println("===============MD5==============="); } } } /** * 对一个文件获取md5值 * @return md5串 */ public static String getMD5(File file) { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); byte[] buffer = new byte[8192]; int length; while ((length = fileInputStream.read(buffer)) != -1) { MD5.update(buffer, 0, length); } return new String(Hex.encodeHex(MD5.digest())); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } finally { try { if (fileInputStream != null) fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
百度网盘:http://pan.baidu.com/s/1bgUFHc
相关文章推荐
- Java对象分配简要流程
- java反射的一些调试
- MyEclipse 每次保存都要building workspace的解决方法
- 【转】Java并发编程:深入剖析ThreadLocal
- 数据流形式参数 访问counter
- ubuntu 使用Eclipse对FFmpeg进行调试
- maven web 项目中启动报错java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener
- java中的SuppressWarnings注解的用法
- java IO流读写文件
- java并发库之Executors常用的创建ExecutorService的几个方法说明
- Java常见异常(Runtime Exception )小结(转)
- java socket 通信编程
- 全排列_蓝桥杯问题+一点对递归的看法
- Java中函数参数的传递是值传递还是引用传递
- Java之Filter过滤器
- Java获取来访者IP
- JAVA学习【11】Servlet基础知识
- java 不复制字符串前提下替换字符串中的空格算法实现O(n)
- 在Eclipse的Debug页签中设置虚拟机参数
- java最简单方法配置统一异常页面