文件MD5 发表于 2017-11-22 | 分类于 Java *根据文件获取MD5 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061//获取单个文件的MD5的值 public static String mD5(MultipartFile file) { if (file == null && StringUtil.isNull(file.getOriginalFilename())) { return null; } MessageDigest digest = null; InputStream fis = null; byte buffer[] = new byte[1024]; int len; try { digest = MessageDigest.getInstance("MD5"); fis = file.getInputStream(); while ((len = fis.read(buffer, 0, 1024)) != -1) { digest.update(buffer, 0, len); } } catch (Exception e) { e.printStackTrace(); return ""; } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return bytesToHexString(digest.digest()); } private static String bytesToHexString(byte[] src) { if (src == null || src.length <= 0) { return null; } StringBuilder stringBuilder = new StringBuilder(); for (byte aSrc : src) { int v = aSrc & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } Donate comment here 打赏 微信支付 支付宝 本文作者: Banksy Zhuang 本文链接: https://blog.zhuangzexin.top/2017/11/22/文件MD5/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!