import java.io.*;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Properties;/*** 读取一个图片MD5,和文件夹里的所有图片进行比较,返回相同的匹配信息* @author silence*/
public class MD5 {private static final HashMap<String, File> map = new HashMap<>();// 需要查找的图片路径,查找是否有HQ命名的图片private static File oneFile = new File("operate\\HQ");//配置文件路径private static File configFile = new File("operate\\HQ\\config.txt");//写入错误信息的文件路径private static File errorFile = new File("operate\\HQ\\run.txt");//被查找的图片文件路径private static File listFile = null;//统计查找次数的计数器private static int count = 1;/*** 传入一个文件流对象,读取MD5返回** @param file 文件流对象* @return 读取MD5返回* @throws IOException io异常*/private static String getMd5ByFile(File file) throws IOException {String md5Value = null;if (!file.canRead()) {return null;}try (FileInputStream in = new FileInputStream(file)) {MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());MessageDigest md5 = MessageDigest.getInstance("MD5");md5.update(byteBuffer);BigInteger bi = new BigInteger(1, md5.digest());md5Value = bi.toString(16);} catch (Exception e) {e.printStackTrace();}return md5Value;}/*** 主方法=============================================*/public static void main(String[] args) throws IOException {//在这里读取判断单个文件图片,是否有效String oneMd5 = getOneMd5(oneFile);//1.判断两个文件路径是否有效,无效退出if (!ioFile() || oneMd5 == null) {System.out.println("程序退出!!!");System.exit(1);}//2.被查找的图片文件路径,读取到mapgetFileMd5(listFile);System.out.println("文件读完成共读取" + count + "条信息....");//判断=======================Properties properties = new Properties();FileWriter writer = new FileWriter(errorFile);if (map.containsKey(oneMd5)) {//如果有符合条件的值,想配置文件写成功信息properties.setProperty("所匹配的文件名", map.get(oneMd5).getName());properties.setProperty("所匹配的文件路径", map.get(oneMd5).toString());properties.setProperty("所在文件夹路径", map.get(oneMd5).getParent());properties.setProperty("匹配成功,共匹配", count + "个文件信息");System.out.println("匹配成功,共匹配" + count + "个文件信息");} else {//如果没有符合条件的值,向配置文件写失败信息properties.setProperty("匹配失败,共匹配", count + "个文件信息");System.out.println("匹配失败,共匹配" + count + "个文件信息");}properties.store(writer, null);writer.close();System.out.println("程序正常结束");}/*** 读取需要查找文件的md5** @param oneFile 0* @return MD5*/private static String getOneMd5(File oneFile) throws IOException {//如果没有这个文件夹,返回nullif (!oneFile.canRead()) {return null;}File[] files = oneFile.listFiles();for (File f : files) {//需要是文件的String name = f.getName().split("\\.")[0];if (f.isFile() && "HQ".equals(name)) {return getMd5ByFile(f);}}//如果没有这个文件信息Properties p = new Properties();System.out.println("该文件没有名为HQ的图片信息,请把需要查找的图片改名为HQ,放到当前文件下");p.setProperty("程序结束", "该文件没有名为HQ的图片信息,请把需要查找的图片改名为HQ,放到当前文件下");FileWriter writer = new FileWriter(errorFile);p.store(writer, null);writer.close();return null;}/*** 判断是否存在配置文件,存在返回true 不存在,创建,返回false** @return 存在返回true 不存在,创建,返回false*/private static boolean ioFile() throws IOException {//判断文件配置路径是否存在,并且可读if (!configFile.canRead()) {System.out.println("配置文件不存在!!!即将创建一个空配置文件....");FileWriter writer = new FileWriter(configFile);Properties p = new Properties();p.setProperty("被查找的图片文件路径", "");p.store(writer, null);writer.close(); //关闭文件流return false;} else {String value1, value2;Properties p = new Properties();try (FileReader reader = new FileReader(configFile)) {System.out.println("配置文件存在,正在读取配置信息......");//读取配置文件p.load(reader);value1 = (String) p.get("被查找的图片文件路径");}System.out.println("正在判断文件路径是否为空....");if (value1 == null || "".equals(value1)) {System.out.println("文件路径为空!!!");return false;}if (!(new File(value1).canRead())) {System.out.println("文件路径无效!!!");return false;}listFile = new File(value1);System.out.println("图片路径有效进入下一步操作....");return true;}}/*** 读取所有文件,存储md5** @param file 文件* @throws IOException io异常*/private static void getFileMd5(File file) throws IOException {if (!file.canRead()) {//如果文件不可读,或不是正确的文件路径System.out.println("文件不可读,或不是正确的文件路径");return;}//返回此文件夹包含的所有文件try {File[] files = file.listFiles();assert files != null;for (File f : files) {//如果是一个文件,获取MD5存入到mapif (f.isFile()) {String md5 = getMd5ByFile(f);System.out.println("成功读取第" + count + "个");count++;map.put(md5, f);} else {getFileMd5(f);}}} catch (Exception e) {e.printStackTrace();}}}
================
1.在HQ文件夹内存入需要查找的HQ命名的图片
2.在HQ文件夹中config.txt 中填入需要查找的文件夹名(文件夹)
列:C:\Users\Administrator\Desktop\Demo小案例\共享文件夹\IEDA背景图片\img\新建文件夹 (2)
必须为双斜杠
3.点击启动,运行完成后信息会存入HQ文件夹下run.txt文件中
#Fri Apr 16 18:31:12 CST 2021
所在文件夹路径=D:\develop\jdk-11\conf\management
所匹配的文件路径=D:\develop\jdk-11\conf\management\jmxremote.access
匹配成功,共匹配=1943个文件信息
所匹配的文件名=jmxremote.access
#Fri Apr 16 17:30:46 CST 2021
被查找的图片文件路径=D:\develop
C:\Users\Administrator\Desktop\Demo小案例\共享文件夹\IEDA背景图片\img\新建文件夹 (2)