MD5Util.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package org.jeecg.common.util;
  2. import org.apache.commons.codec.digest.DigestUtils;
  3. import java.io.FileInputStream;
  4. import java.security.MessageDigest;
  5. import java.security.NoSuchAlgorithmException;
  6. public class MD5Util {
  7. private static ThreadLocal<MessageDigest> MD5 = new ThreadLocal<MessageDigest>() {
  8. @Override
  9. protected MessageDigest initialValue() {
  10. try {
  11. return MessageDigest.getInstance("MD5");
  12. } catch (NoSuchAlgorithmException e) {
  13. throw new IllegalStateException("No md5 algorithm found");
  14. }
  15. }
  16. };
  17. public static String toHexStr(byte[] bytes) {
  18. int j = bytes.length;
  19. char[] str = new char[j * 2];
  20. int k = 0;
  21. for (int i = 0; i < j; i++) {
  22. byte byte0 = bytes[i];
  23. str[k++] = HEX_DIGITS_CHAR[byte0 >>> 4 & 0xf];
  24. str[k++] = HEX_DIGITS_CHAR[byte0 & 0xf];
  25. }
  26. return String.valueOf(str);
  27. }
  28. public static String getMd5(String url) {
  29. MessageDigest md5 = MD5.get();
  30. md5.reset();
  31. return toHexStr(md5.digest(url.getBytes()));
  32. }
  33. public static String byteArrayToHexString(byte b[]) {
  34. StringBuffer resultSb = new StringBuffer();
  35. for (int i = 0; i < b.length; i++){
  36. resultSb.append(byteToHexString(b[i]));
  37. }
  38. return resultSb.toString();
  39. }
  40. private static String byteToHexString(byte b) {
  41. int n = b;
  42. if (n < 0) {
  43. n += 256;
  44. }
  45. int d1 = n / 16;
  46. int d2 = n % 16;
  47. return hexDigits[d1] + hexDigits[d2];
  48. }
  49. public static String MD5Encode(String origin, String charsetname) {
  50. String resultString = null;
  51. try {
  52. resultString = new String(origin);
  53. MessageDigest md = MessageDigest.getInstance("MD5");
  54. if (charsetname == null || "".equals(charsetname)) {
  55. resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
  56. } else {
  57. resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
  58. }
  59. } catch (Exception exception) {
  60. }
  61. return resultString;
  62. }
  63. public static String md5Encode(String origin, String charsetname) {
  64. String resultString = null;
  65. try {
  66. resultString = new String(origin);
  67. MessageDigest md = MessageDigest.getInstance("MD5");
  68. if (charsetname == null || "".equals(charsetname)) {
  69. resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
  70. } else {
  71. resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
  72. }
  73. } catch (Exception exception) {
  74. }
  75. return resultString;
  76. }
  77. private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
  78. "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
  79. private static final String[] HEX_DIGITS = {"0", "1", "2", "3", "4", "5",
  80. "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
  81. private static final char[] HEX_DIGITS_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  82. public static String getFileMd5(String path) throws Exception {
  83. return DigestUtils.md5Hex(new FileInputStream(path));
  84. }
  85. public static void main(String[] args) throws Exception {
  86. System.out.println(getFileMd5("D:\\mnt\\0427有缘3-帆楠E打车-同城热恋-1587982341839.mp4"));
  87. System.out.println(getFileMd5("D:\\mnt\\0427有缘4-帆楠E洗脚-同城热恋-1593661309704.mp4"));
  88. }
  89. }