|
| 1 | +package com.algorithm.digest; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.io.IOException; |
| 7 | +import java.math.BigInteger; |
| 8 | +import java.nio.MappedByteBuffer; |
| 9 | +import java.nio.channels.FileChannel.MapMode; |
| 10 | +import java.security.MessageDigest; |
| 11 | +import java.security.NoSuchAlgorithmException; |
| 12 | + |
| 13 | +/** |
| 14 | + * 快速获取文件MD5 |
| 15 | + * |
| 16 | + * @author robert |
| 17 | + * |
| 18 | + */ |
| 19 | +public class DigestUtil { |
| 20 | + public static String fastDigest(File file) { |
| 21 | + FileInputStream input = null; |
| 22 | + try { |
| 23 | + MessageDigest md = MessageDigest.getInstance("md5"); |
| 24 | + input = new FileInputStream(file); |
| 25 | + long len = file.length(); |
| 26 | + long start = 0; |
| 27 | + if (file.length() > 1024 * 1024 * 1024 * 2) { |
| 28 | + len = 1024 * 1024 * 1024 * 1; |
| 29 | + start = len; |
| 30 | + } |
| 31 | + MappedByteBuffer mb = input.getChannel().map(MapMode.READ_ONLY, 0, len); |
| 32 | + md.update(mb); |
| 33 | + if (start != 0) { |
| 34 | + FileInputStream in = null; |
| 35 | + byte buffer[] = new byte[1024 * 1024 * 10]; |
| 36 | + try { |
| 37 | + in = new FileInputStream(file); |
| 38 | + in.skip(len); |
| 39 | + int length = 0; |
| 40 | + while ((length = in.read(buffer)) != -1) { |
| 41 | + md.update(buffer, 0, length); |
| 42 | + } |
| 43 | + in.close(); |
| 44 | + } catch (Exception e) { |
| 45 | + e.printStackTrace(); |
| 46 | + return null; |
| 47 | + } |
| 48 | + } |
| 49 | + return bytesTohex(md.digest()); |
| 50 | + |
| 51 | + } catch (NoSuchAlgorithmException e) { |
| 52 | + // TODO Auto-generated catch block |
| 53 | + e.printStackTrace(); |
| 54 | + } catch (FileNotFoundException e) { |
| 55 | + // TODO Auto-generated catch block |
| 56 | + e.printStackTrace(); |
| 57 | + } catch (IOException e) { |
| 58 | + // TODO Auto-generated catch block |
| 59 | + e.printStackTrace(); |
| 60 | + } finally { |
| 61 | + try { |
| 62 | + if (input != null) { |
| 63 | + input.close(); |
| 64 | + } |
| 65 | + } catch (IOException e) { |
| 66 | + // TODO Auto-generated catch block |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + public static String getFileMD5(File file) { |
| 75 | + MessageDigest digest = null; |
| 76 | + FileInputStream in = null; |
| 77 | + byte buffer[] = new byte[1024 * 1024 * 10]; |
| 78 | + int len; |
| 79 | + try { |
| 80 | + digest = MessageDigest.getInstance("MD5"); |
| 81 | + in = new FileInputStream(file); |
| 82 | + in.read(buffer, 0, 1); |
| 83 | + while ((len = in.read(buffer)) != -1) { |
| 84 | + digest.update(buffer, 0, len); |
| 85 | + } |
| 86 | + in.close(); |
| 87 | + } catch (Exception e) { |
| 88 | + e.printStackTrace(); |
| 89 | + return null; |
| 90 | + } |
| 91 | + return bytesTohex(digest.digest()); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * 字节转换为Stirng |
| 96 | + * |
| 97 | + * 快速简单实现方式 BigInteger bigInt = new BigInteger(1, digest.digest()); <br> |
| 98 | + * return bigInt.toString(16); |
| 99 | + * |
| 100 | + * @param bytes |
| 101 | + * @return |
| 102 | + */ |
| 103 | + public static String bytesTohex(byte[] bytes) { |
| 104 | + StringBuffer strbuf = new StringBuffer(); |
| 105 | + for (int i = 0; i < bytes.length; i++) { |
| 106 | + strbuf.append(Character.forDigit((bytes[i] & 0xf0) >> 4, 16)); |
| 107 | + strbuf.append(Character.forDigit((bytes[i] & 0x0f), 16)); |
| 108 | + } |
| 109 | + return strbuf.toString(); |
| 110 | + |
| 111 | + } |
| 112 | +} |
0 commit comments