图片处理依赖
<dependency><groupId>net.coobird</groupId><artifactId>thumbnailator</artifactId><version>0.4.8</version><scope>test</scope></dependency>
压缩工具类
package com.example.demo;import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import net.coobird.thumbnailator.Thumbnails;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import java.io.File;
import java.util.*;
import java.util.stream.Collectors;@SpringBootTest
public class ExcelDemo {@Testpublic void test2() {try {//输入输出文件夹位置 图片前缀有压缩为已压缩图片File file1 = new File("C:\\Users\\Administrator\\Desktop");List<String> strings = Arrays.asList("bmp,jpg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,WMF,webp,avif,apng".split(","));//strings.forEach(System.out::println);//Arrays.stream(file1.listFiles()).forEach(System.out::println);List<File> list = Arrays.stream(file1.listFiles()).filter(s -> {if (s.toString().contains(".")) {String substring = s.toString().substring(s.toString().indexOf(".")+1);return strings.contains(substring);}return false;}).collect(Collectors.toList());if (CollUtil.isNotEmpty(list)) {List<String> collect = list.stream().map(s -> s.getName()).filter(s->s.startsWith("压缩后")).map(s->s.substring(3)).collect(Collectors.toList());for (File file : list) {String name = file.getName();if (!name.startsWith("压缩后") && !collect.contains(name)) {Thumbnails.of(file).size(38, 38).keepAspectRatio(false).toFile(file1 + "\\压缩后" + name);System.out.println("执行");}}}} catch (Exception e) {e.printStackTrace();}System.out.println("结束");}
}