获取文件的md5值

public class MD5Utils {

    public static void main(String[] args) throws Exception{
        File file = new File("D:\\msdia80.dll");
        String md5 = MD5Utils.getMD5(new FileInputStream(file));
        System.out.println(md5);
    }


    /**
     * 获取文件的MD5值
     */
    public static String getMD5(InputStream in) {
        MessageDigest digest = null;
        byte buffer[] = new byte[1024 * 1024];
        int len;

        try {
            digest = MessageDigest.getInstance("MD5");
            while ((len = in.read(buffer, 0, 1024 * 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new SunawException("获取文件的MD5错误");
        } catch (IOException e) {
            e.printStackTrace();
            throw new SunawException("获取文件的MD5错误");
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    }
}

 

posted @ 2019-11-01 10:29  陌路×难飞  阅读(1049)  评论(0编辑  收藏  举报