源码文件测试代码运行结果
源码文件
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];
MessageDigest complete = MessageDigest.getInstance(
"MD5");
if(in !=
null){
int numRead;
do {
numRead = in.read(buffer);
if (numRead >
0) {
complete.update(buffer,
0, numRead);
}
}
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 ++){
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);
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