wangy 发表于 2021-6-30 22:16:04

【Java】获取文件MD5值

public static String getMD5(File file)
        {
                String md5 = "";
                try
                {
            FileInputStream fis = new FileInputStream(file);
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] buffer = new byte;
            int length = -1;
            while ((length = fis.read(buffer, 0, 1024)) != -1)
                        {
                md.update(buffer, 0, length);
            }
            BigInteger bigInt = new BigInteger(1, md.digest());
                        md5 = bigInt.toString(16);
      } catch (FileNotFoundException e) {
            e.printStackTrace();
      } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
      } catch (IOException e) {
            e.printStackTrace();
      }
                return md5;
    }
页: [1]
查看完整版本: 【Java】获取文件MD5值