All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.iceolive.coswebdeploy.util.Md5Util Maven / Gradle / Ivy

The newest version!
package com.iceolive.coswebdeploy.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;

/**
 * @author:wangmianzhe
 **/
@Slf4j
public class Md5Util {

    /**
     * 获取一个文件的md5
     * @param file 文件
     * @return md5
     */
    public static String getMD5(File file) {
        FileInputStream fileInputStream = null;
        try {
            MessageDigest MD5 = MessageDigest.getInstance("MD5");
            fileInputStream = new FileInputStream(file);
            byte[] buffer = new byte[8192];
            int length;
            while ((length = fileInputStream.read(buffer)) != -1) {
                MD5.update(buffer, 0, length);
            }
            return new String(Hex.encodeHex(MD5.digest()));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (IOException e) {
                log.error(e.toString(), e);
            }
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy