fix:优化图片处理

上级 2716b0ca
......@@ -12,6 +12,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* 删除博客中不存在的照片
......@@ -25,7 +26,7 @@ public class FileContainsUtil {
/**
* 默认图片不存在
*/
private static boolean IS_EXIST = false;
private static ThreadLocal<Boolean> IS_EXIST = ThreadLocal.withInitial(() -> false);
/**
* 图片路径
*/
......@@ -61,49 +62,95 @@ public class FileContainsUtil {
final List<String> picNames = getPicName(PIC_PATH);
final int size = picNames.size();
log.info("图片总数为{}", size);
if (CollectionUtil.isNotEmpty(picNames)) {
outerloop:
for (int i = 0; i < size; i++) {
String picName = picNames.get(i);
List<List<String>> groupedPicNames = dataGroup(picNames);
moreThread(groupedPicNames, isNotExist);
log.info("不存在图片总数为{}", isNotExist.size());
stopWatch.stop();
//毫秒输出
log.info("耗时统计信息:{}", JSON.toJSONString(stopWatch.getTaskInfo()));
log.info("耗时秒数:{}", JSON.toJSONString(stopWatch.getTotalTimeSeconds()));
log.info("耗时分钟数:{}", JSON.toJSONString(stopWatch.getTotalTimeSeconds() / 60));
}
/**
* 多线程处理图片任务
*
* @param groupedPicNames
* @param isNotExist
*/
private static void moreThread(List<List<String>> groupedPicNames, List<String> isNotExist) {
// 创建并启动4个线程来处理每个组的任务
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 6; i++) {
final List<String> group = groupedPicNames.get(i);
Thread thread = new Thread(() -> {
// 在这里执行处理图片组的任务
if (CollectionUtil.isNotEmpty(group)) {
for (String picName : group) {
// 处理图片文件 picName
//是白名单里面的图片,直接忽略
for (String picPathWhitelist : PIC_PATH_WHITELISTS) {
if (picPathWhitelist.contains(picName)) {
continue outerloop;
continue;
}
}
//默认不存在
IS_EXIST = false;
//指定类型的文件
List<String> suffix = new ArrayList<>();
suffix.add(".md");
IS_EXIST.set(false);
//包含某个字符串
traverseFolder(BLOG_FOLDER1, suffix, picName);
traverseFolder(BLOG_FOLDER2, suffix, picName);
traverseFolder(BLOG_FOLDER3, suffix, picName);
try {
traverseFolder(BLOG_FOLDER1, picName);
traverseFolder(BLOG_FOLDER2, picName);
traverseFolder(BLOG_FOLDER3, picName);
} catch (Exception e) {
e.printStackTrace();
}
//文件不存在
if (!IS_EXIST) {
if (!IS_EXIST.get()) {
isNotExist.add(picName);
deletePic(PIC_PATH + picName);
}
}
}
log.info("不存在图片总数为{}", isNotExist.size());
stopWatch.stop();
//毫秒输出
log.info("耗时统计信息:{}", JSON.toJSONString(stopWatch.getTaskInfo()));
log.info("耗时秒数:{}", JSON.toJSONString(stopWatch.getTotalTimeSeconds()));
log.info("耗时分钟数:{}", JSON.toJSONString(stopWatch.getTotalTimeSeconds() / 60));
});
thread.start();
threads.add(thread);
}
// 等待所有线程完成
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 数据分组
*
* @param picNames
* @return
*/
private static List<List<String>> dataGroup(List<String> picNames) {
int groupSize = picNames.size() / 6; // 计算每组的大小
List<List<String>> groupedPicNames =
picNames.stream()
.collect(Collectors.groupingBy(e -> picNames.indexOf(e) / groupSize))
.values()
.stream()
.collect(Collectors.toList());
return groupedPicNames;
}
/**
* 获取文件
*
* @param path
* @param suffixs
* @param word
* @throws IOException
*/
public static void traverseFolder(String path, List<String> suffixs, String word) throws Exception {
public static void traverseFolder(String path, String word) throws Exception {
File file = new File(path);
if (file.exists()) {
//获取文件夹下的文件
......@@ -112,11 +159,10 @@ public class FileContainsUtil {
for (File file2 : files) {
//是否是文件夹
if (file2.isDirectory()) {
traverseFolder(file2.getAbsolutePath(), suffixs, word);
traverseFolder(file2.getAbsolutePath(), word);
} else {
for (String suffix : suffixs) {
//包含md结尾的文件
if (file2.getAbsolutePath().contains(suffix)) {
if (file2.getAbsolutePath().contains(".md")) {
getParams(file2.getAbsolutePath(), word);
}
}
......@@ -124,7 +170,6 @@ public class FileContainsUtil {
}
}
}
}
/**
* 判断文件是否存在
......@@ -141,7 +186,7 @@ public class FileContainsUtil {
//判断是否包含方法名称,即指定字符串
if (string.contains(word)) {
//文件存在
IS_EXIST = true;
IS_EXIST.set(true);
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册