干货!java文件上传判重姿势浅谈

一、场景:文件上传,用户极有可能上传重复文件,内容完全一致。如果对上传的文件未做任何处理,对于文件存储系统来说将是灾难,大量重复的数据,如果允许上传大文件,那么对于存储资源将是巨大的浪费。对于重复的文件,只需要复制相应的访问地址即可,源文件可无需上传,既减轻了网络带宽压力,也减少了存储容量的压力。

二、应对:

1、通过文件名判重。非特殊情况下,不会采用这种方案,理由跟人同名一样,文件名很容易重复,随着用户上升,概率会变大。采用此方案极易导致不能达到判重的目的。

2、读取文件头加部分内容。这种方案可以解决百分之五十的问题,缺点是随着量的上升,重复的概率依然存在。

3、读取文件内容,进行hash计算,通常情况下,这种方案比较可靠,出现误判的概率低。一些分布式文件系统,如fastdfs等也是采取hash的方式进行文件判重。

三、方案

开发语言:java  JDK 1.8 IDE:eclipse

机器配置:i5双核  内存4G 64位

四、代码实现

1、org.apache.commons.codec.digest.DigestUtils

@Test
  public void test1()
    String path = "your file path";
    try {
      long begin = System.currentTimeMillis();
      String md5 = DigestUtils.md5Hex(new FileInputStream(path));
      long end = System.currentTimeMillis();
      System.out.println(md5);
      System.out.println("time:" + ((end - begin) / 1000) + "s");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

2、自定义缓冲区实现

@Test
  public void test2() {
    String path ="file path";
    long begin = System.currentTimeMillis();
    BigInteger bi = null;
    try {
      byte[] buffer = new byte[8192 * 10];
      int len = 0;
      MessageDigest md = MessageDigest.getInstance("MD5");
      File f = new File(path);
      FileInputStream fis = new FileInputStream(f);
      while ((len = fis.read(buffer)) != -1) {
        md.update(buffer, 0, len);
      }
      fis.close();
      byte[] b = md.digest();
      bi = new BigInteger(1, b);
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    String md5 = bi.toString(16);
    long end = System.currentTimeMillis();
    System.out.println(md5);
    System.out.println("time:" + ((end - begin) / 1000) + "s");
  }

3、com.twmacinta.util.MD5

@Test
  public void test3() {
    String path ="file path";
    long begin = System.currentTimeMillis();
    try {
      String md5 = MD5.asHex(MD5.getHash(new File(path)));
      long end = System.currentTimeMillis();
      System.out.println(md5);
      System.out.println("time:" + ((end - begin) / 1000) + "s");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

4、NIO读取


public class MD5FileUtil {
  /**
   * 默认的密码字符串组合,用来将字节转换成 16 进制表示的字符,apache校 验下载的文件的正确性用的就是默认的这个组合
   */
  protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
      'f' };

  protected static MessageDigest messagedigest = null;
  static {
    try {
      messagedigest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }
  }

  /**
   * 生成文件的md5校验值
   * @param file 文件路径
   * @return MD5码返回
   * @throws IOException
   */
  public static String getFileMD5(File file) throws IOException {
    String encrStr = "";
    // 读取文件
    FileInputStream fis = new FileInputStream(file);
    // 当文件<2G可以直接读取
    if (file.length() <= Integer.MAX_VALUE) {
      encrStr = getMD5Lt2G(file, fis);
    } else { // 当文件>2G需要切割读取
      encrStr = getMD5Gt2G(fis);
    }
    fis.close();
    return encrStr;
  }

  /**
   * 小于2G文件
   * 
   * @param fis 文件输入流
   * @return
   * @throws IOException
   */
  public static String getMD5Lt2G(File file, FileInputStream fis) throws IOException {
    // 加密码
    String encrStr = "";
    FileChannel ch = fis.getChannel();
    MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
    messagedigest.update(byteBuffer);
    encrStr = bufferToHex(messagedigest.digest());
    return encrStr;
  }

  /**
   * 超过2G文件的md5算法
   * 
   * @param fileName
   * @param InputStream
   * @return
   * @throws Exception
   */
  public static String getMD5Gt2G(InputStream fis) throws IOException {
    // 自定义文件块读写大小,一般为4M,对于小文件多的情况可以降低
    byte[] buffer = new byte[1024 * 1024 * 4];
    int numRead = 0;
    while ((numRead = fis.read(buffer)) > 0) {
      messagedigest.update(buffer, 0, numRead);
    }
    return bufferToHex(messagedigest.digest());
  }

  /**
   * 
   * @param bt           文件字节流
   * @param stringbuffer 文件缓存
   */
  private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
    // 取字节中高 4 位的数字转换, >>> 为逻辑右移,将符号位一起右移,此处未发现两种符号有何不同
    char c0 = hexDigits[(bt & 0xf0) >> 4];
    // 取字节中低 4 位的数字转换
    char c1 = hexDigits[bt & 0xf];
    stringbuffer.append(c0);
    stringbuffer.append(c1);
  }

  private static String bufferToHex(byte bytes[], int m, int n) {
    StringBuffer stringbuffer = new StringBuffer(2 * n);
    int k = m + n;
    for (int l = m; l < k; l++) {
      appendHexPair(bytes[l], stringbuffer);
    }
    return stringbuffer.toString();
  }

  private static String bufferToHex(byte bytes[]) {
    return bufferToHex(bytes, 0, bytes.length);
  }

  /**
   * 判断字符串的md5校验码是否与一个已知的md5码相匹配
   * @param password  要校验的字符串
   * @param md5PwdStr 已知的md5校验码
   * @return
   */
  public static boolean checkPassword(String password, String md5PwdStr) {
    String s = getMD5String(password);
    return s.equals(md5PwdStr);
  }

  /**
   * 生成字符串的md5校验值
   * @param s
   * @return
   */
  public static String getMD5String(String s) {
    return getMD5String(s.getBytes());
  }

  /**
   * 生成字节流的md5校验值
   * @param s
   * @return
   */
  public static String getMD5String(byte[] bytes) {
    messagedigest.update(bytes);
    return bufferToHex(messagedigest.digest());
  }
  
  public static void main(String[] args) throws IOException {
    String path ="path";
    File big = new File(path);
    long begin = System.currentTimeMillis();
    String md5 = getFileMD5(big);

    long end = System.currentTimeMillis();
    System.out.println("md5:" + md5);
    System.out.println("time " + (end - begin));
    System.out.println("time:" + ((end - begin) / 1000) + "s");

  }
}

五、测试结果

方式/时间

304KB

31.2MB

69.5MB

600MB

3.09G

apache

37ms

489ms

1121ms

8987ms

45699ms

缓冲区

4ms

134ms

292ms

9393ms

45993ms

md5

19ms

173ms

338ms

9021ms

48427ms

nio去读

22ms

165ms

320ms

9815ms

45347ms

600M以下:缓冲区 > NIO > MD5 > Apache

600M以上:Apache>缓冲区>NIO>MD5

六、总结

以上数据取样比较分散,可以采用均匀分布样本测试的结果可能更加特近真实效果,也可能得出一个转折点,可根据不同的数据量采用不同的生成模式,其效率有一定差别:

有兴趣的朋友私下可以进行多次测试,可得出更真实的结果

数据以小文件为主的,使用缓冲区生成MD5的方式效率更高,而到了G级别的文件,采用apache的生成方式更高效。上述结果仅供参考,实际情况下请各位根据需要采用不同的生成方式。如有更高效的生成方式,欢迎交流。

图片

おすすめ

転載: blog.csdn.net/yelangkingwuzuhu/article/details/113839280