计算文件的MD5

源码文件

package com.lyc.noJs.util;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class ImgMD5Util {

    private String imgPath = null;

    /**
     * 完成ImgMD5Util工具类的初始化操作
     * @return
     */
    public static ImgMD5Util newImgMD5Util(){
        return new ImgMD5Util();
    }

    /**
     * 有参初始化方法
     * @param imgPath
     * @return
     */
    public static ImgMD5Util newImgMD5Util(String imgPath){
        return new ImgMD5Util(imgPath);
    }

    /**
     * 获取文件输入流
     * @param imgPath
     * @return
     * @throws FileNotFoundException
     */
    public InputStream getInputStream(String imgPath) throws FileNotFoundException {
        return new FileInputStream(imgPath);
    }

    /**
     * 获取图片文件的Digest
     * @param in
     * @return
     * @throws NoSuchAlgorithmException
     * @throws IOException
     */
    public byte[] getImgDigest(InputStream in) throws NoSuchAlgorithmException, IOException {
        byte[] buffer = new byte[1024];
        //如果想使用SHA-1或SHA-256,则传入SHA-1,SHA-256
        MessageDigest complete = MessageDigest.getInstance("MD5");
        if(in != null){
            int numRead;
            do {
                numRead = in.read(buffer);    //从文件读到buffer,最多装满buffer
                if (numRead > 0) {
                    complete.update(buffer, 0, numRead);  //用读到的字节进行MD5的计算,第二个参数是偏移量
                }
            } while (numRead != -1);
            in.close();
        } else {
            log.info("图片读取失败!");
            return null;
        }
        return complete.digest();
    }

    /**
     * 将byte[]类型的MD5转换成String类型的
     * @param bytes
     * @return
     */
    public String toMD5String(byte[] bytes){
        StringBuffer mD5String = new StringBuffer();
        for (int i = 0;i < bytes.length;i ++){
            //加0x100是因为有的b[i]的十六进制只有1位
            mD5String.append(Integer.toString( ( bytes[i] & 0xff ) + 0x100, 16).substring(1));
        }
        return mD5String.toString();
    }

    /**
     * 获取图片的MD5值
     * @param imgPath
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    public String getMD5(String imgPath) throws IOException, NoSuchAlgorithmException {
        //获取图片文件的输入流
        InputStream in = getInputStream(imgPath);
        //获取图片文件的哈希值
        byte[] bytes = getImgDigest(in);
        //将byte[]类型的MD5转换成String类型的
        return toMD5String(bytes);
    }

    /**
     * 计算文件的MD5值
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    public String getMD5() throws IOException, NoSuchAlgorithmException{
        return getMD5(imgPath);
    }

}

测试代码

package com.lyc.demo;

import com.lyc.noJs.util.ImgMD5Util;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class ImgMD5Test {

    /**
     * 测试无参构造函数
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    @Test
    public void test() throws IOException, NoSuchAlgorithmException {
        ImgMD5Util imgMD5Util = ImgMD5Util.newImgMD5Util();
        String md5 = imgMD5Util.getMD5("D:\\WorkSpace\\no-js-parent\\no-js\\target\\no-js\\upload\\2018\\04\\09\\1523257445593.jpg");
        log.info(md5);
    }

    /**
     * 测试有参构造函数
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    @Test
    public void test2() throws IOException, NoSuchAlgorithmException {
        ImgMD5Util imgMD5Util = ImgMD5Util.newImgMD5Util("D:\\WorkSpace\\no-js-parent\\no-js\\target\\no-js\\upload\\2018\\04\\09\\1523257445593.jpg");
        String md5 = imgMD5Util.getMD5();
        log.info(md5);
    }

}

运行结果

2018-04-09 15:19:28  INFO main com.lyc.demo.ImgMD5Test.test(ImgMD5Test.java:23) - 174b3302a43ad5fdd972a3d2e99ff78e
2018-04-09 15:19:28  INFO main com.lyc.demo.ImgMD5Test.test2(ImgMD5Test.java:35) - 174b3302a43ad5fdd972a3d2e99ff78e

猜你喜欢

转载自blog.csdn.net/zzy1078689276/article/details/79867160