美文网首页
获取文件MD5值

获取文件MD5值

作者: 兜里揣着酱油钱 | 来源:发表于2018-01-22 19:12 被阅读0次
    • 记录一下获取文件md5值的方法
        /**
        * 获取文件MD5值
        * @param FilePath
        * @return
        */
        static String GetMd5(String FilePath) {
            char hexdigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
            FileInputStream fis = null;
            String sString;
            char str[] = new char[16 * 2];
            int k = 0;
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                fis = new FileInputStream(FilePath);
                byte[] buffer = new byte[1024];
                int length = -1;
                while ((length = fis.read(buffer)) != -1) {
                    md.update(buffer, 0, length);
                }
                byte[] b = md.digest();
                for (int i = 0; i < 16; i++) {
                    byte byte0 = b[i];
                    str[k++] = hexdigits[byte0 >>> 4 & 0xf];
                    str[k++] = hexdigits[byte0 & 0xf];
                }
                fis.close();
                sString = new String(str);
                return sString;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                        fis = null;
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:获取文件MD5值

          本文链接:https://www.haomeiwen.com/subject/irdraxtx.html