用JAVA删除文件夹中的相同照片

用JAVA编写一个成语,可以把一个文件夹中的几张照片删除,并只保留一张。全部的代码。相同的照片格式都有相同的地方。

if (!Func.equals(businessLicensePath, customerUpdateDTO.getBusinessLicense())) {
attachmentService.deleteFile(customer.getId(), AttachmentSourceEnum.CUSTOMER_BUSINESS_LICENSE_ATTACHMENT.getCode());
if (Func.isNotBlank(customerUpdateDTO.getBusinessLicense())) {
attachmentService.insertFile(customerUpdateDTO.getBusinessLicense(), customer.getId(),
AttachmentSourceEnum.CUSTOMER_BUSINESS_LICENSE_ATTACHMENT.getCode(), StrUtil.EMPTY, customerUpdateDTO.getBusinessLicenseName());
}
Attachment old = new Attachment();
if (Func.isNotBlank(businessLicensePath)) {
old.setPath(businessLicense.get(0).getPath());
old.setOriginalName(businessLicense.get(0).getOriginalName());
} else {
old = null;
}
Attachment present = new Attachment();
if (Func.isNotBlank(customerUpdateDTO.getBusinessLicense())){
present.setPath(customerUpdateDTO.getBusinessLicense());
present.setOriginalName(customerUpdateDTO.getBusinessLicenseName());
} else {
present = null;
}
operationIds.add(operationRecordService.saveFileRecord(old, present, customerUpdateDTO.getId(),
operationRecordType, OperationTypeEnum.UPDATE.getCode(), StrUtil.EMPTY, AttachmentSourceEnum.CUSTOMER_BUSINESS_LICENSE_ATTACHMENT.getCode()));
}
return operationIds;

相同照片是完全相同,还是有清晰度,亮度的差异


  @Test
    public void test02(){
        List<String> md5List = new ArrayList<>();
        try (Stream<Path> stream = Files.list(Paths.get("D://image"))) {
            List<Path> paths = stream
                    .collect(Collectors.toList());
            for (Path path : paths) {
                byte[] bytes = Files.readAllBytes(path);
                MessageDigest messageDigest = MessageDigest.getInstance("MD5");
                messageDigest.update(bytes);
                String md5 = new BigInteger(1, messageDigest.digest()).toString();
                if(md5List.contains(md5)){
                    Files.delete(path);
                }
                md5List.add(md5);
            }
        } catch (IOException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

将图片字节获取之后进行md5加密就能生成一串字符,相同的就删掉就行了

public static void main(String[] args) throws Exception {
        byte[] bytes = Files.readAllBytes(new File("d:/sss.png").toPath());
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] digest = md5.digest(bytes);
        String md5Str = new BigInteger(1, digest).toString(16);
        System.out.println(md5Str);
    }

1、图片的名称前缀相同,比如xxx.png、xxx1.png、xxx2.png ... xxxn.png 这样的话就好操作
2、如果图片名没有规律的话,就只有比较图片内容,如果图片比较大,消耗资源很大,性能很差

https://blog.csdn.net/hjh70983704/article/details/119146134
java判断文件夹中的图片是否重复


package com.example.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
 * 判断文件相似度
 */
public class FileDigest {
    /**
     * 获取单个文件的MD5值!
     * @param file
     * @return
     */
    public static String getFileMD5(File file) {
        if (!file.isFile()){
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in=null;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
 
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    }
 
 
 
    public static void main(String[] args) throws IOException {
        String path="D:\\p\\20210209图片采集\\0提供v1.1-处理\\1037#拥抱";
        ArrayList files=FileUtil.getPathFiles(path);
        ArrayList<String> md5s=new ArrayList<String>();
        for (Object file:files){
            File file1 = new File(file+"");
            md5s.add(getFileMD5(file1));
        }
        int md5Size=md5s.size();
        for (int i=0;i<md5Size-1;i++){
            String md5=md5s.get(i);
            for (int j=i+1;j<md5Size;j++){
                if(md5.equals(md5s.get(j))){
                    String srcFile=files.get(i)+"";
                    System.out.println(srcFile);
                    break;
                }
            }
        }
 
    }
 
}

public static void main(String[] args) throws Exception {
byte[] bytes = Files.readAllBytes(new File("d:/sss.png").toPath());
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] digest = md5.digest(bytes);
String md5Str = new BigInteger(1, digest).toString(16);
System.out.println(md5Str);
}