图片上传

master
wany 2026-01-23 09:42:26 +08:00
parent 65af700145
commit a67e76b4a3
1 changed files with 85 additions and 22 deletions

View File

@ -14,6 +14,7 @@ import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -284,30 +285,58 @@ public class FileImporter {
/**
*
*/
// private static List<File> collectImageFiles(File dir) {
// List<File> imgList = new ArrayList<>();
//
// // 处理危险地建房(特殊:有下一层文件夹)
// if (dir.getName().contains("危险地建房")) {
// File[] subDirs = dir.listFiles(File::isDirectory);
// if (subDirs != null) {
// for (File subDir : subDirs) {
// File[] innerImgs = subDir.listFiles((d, name) ->
// name.toLowerCase().endsWith(".jpg") ||
// name.toLowerCase().endsWith(".jpeg") ||
// name.toLowerCase().endsWith(".png"));
// if (innerImgs != null) {
// imgList.addAll(Arrays.asList(innerImgs));
// }
// }
// }
// }
// // 处理普通照片文件夹(房屋结构、弱势群体)
// else {
// File[] imgs = dir.listFiles((d, name) ->
// name.toLowerCase().endsWith(".jpg") ||
// name.toLowerCase().endsWith(".jpeg") ||
// name.toLowerCase().endsWith(".png"));
// if (imgs != null) {
// imgList.addAll(Arrays.asList(imgs));
// }
// }
//
// return imgList;
// }
private static List<File> collectImageFiles(File dir) {
List<File> imgList = new ArrayList<>();
// 处理危险地建房(特殊:有下一层文件夹)
if (dir.getName().contains("危险地建房")) {
File[] subDirs = dir.listFiles(File::isDirectory);
if (subDirs != null) {
for (File subDir : subDirs) {
File[] innerImgs = subDir.listFiles((d, name) ->
name.toLowerCase().endsWith(".jpg") ||
name.toLowerCase().endsWith(".jpeg") ||
name.toLowerCase().endsWith(".png"));
if (innerImgs != null) {
imgList.addAll(Arrays.asList(innerImgs));
}
}
}
}
// 处理普通照片文件夹(房屋结构、弱势群体)
else {
File[] imgs = dir.listFiles((d, name) ->
name.toLowerCase().endsWith(".jpg") ||
name.toLowerCase().endsWith(".jpeg") ||
name.toLowerCase().endsWith(".png"));
// 如果是危险地建房文件夹,需要递归搜索
boolean isDangerHouse = dir.getName().contains("危险地建房");
// 定义图片过滤器
FilenameFilter imgFilter = (d, name) -> {
String lowerName = name.toLowerCase();
return lowerName.endsWith(".jpg") ||
lowerName.endsWith(".jpeg") ||
lowerName.endsWith(".png");
};
if (isDangerHouse) {
// 危险地建房:搜索当前目录及其所有子目录
searchImagesRecursively(dir, imgList, imgFilter);
} else {
// 普通文件夹:只搜索当前目录
File[] imgs = dir.listFiles(imgFilter);
if (imgs != null) {
imgList.addAll(Arrays.asList(imgs));
}
@ -316,6 +345,27 @@ public class FileImporter {
return imgList;
}
// 递归搜索图片
private static void searchImagesRecursively(File dir, List<File> imgList, FilenameFilter imgFilter) {
if (!dir.exists() || !dir.isDirectory()) {
return;
}
// 1. 先收集当前目录的图片
File[] currentImgs = dir.listFiles(imgFilter);
if (currentImgs != null) {
imgList.addAll(Arrays.asList(currentImgs));
}
// 2. 递归搜索子目录
File[] subDirs = dir.listFiles(File::isDirectory);
if (subDirs != null) {
for (File subDir : subDirs) {
searchImagesRecursively(subDir, imgList, imgFilter);
}
}
}
private static ProcessResult handleData(File subDir, String groupId, String watershedCode, String type, String userId) throws Exception {
String subDirName = subDir.getName();
System.out.println("处理文件夹: " + subDirName + " (类型: " + type + ")");
@ -343,9 +393,22 @@ public class FileImporter {
for (Map.Entry<String, List<File>> entry : filesByName.entrySet()) {
String fileName = entry.getKey();
List<File> files = entry.getValue();
// 处理带括号和数字的文件名马小军2
String queryName = fileName;
// 匹配模式:以汉字、字母开头,后面跟任意字符,然后是中文括号内包含数字
// 例如马小军2、张三123、Test5
String pattern = "(.+?)\\\\d+\\$"; // 中文括号
String pattern2 = "(.+?)\\(\\d+\\)$"; // 英文括号
// 尝试匹配并截取
if (fileName.matches(pattern)) {
queryName = fileName.replaceAll("\\\\d+\\$", "");
} else if (fileName.matches(pattern2)) {
queryName = fileName.replaceAll("\\(\\d+\\)$", "");
}
// 根据文件名+流域编码查询业务数据
List<String> businessIds = queryBusinessIds(fileName, watershedCode, type, groupId);
List<String> businessIds = queryBusinessIds(queryName, watershedCode, type, groupId);
if (CollectionUtils.isEmpty(businessIds)) {
System.out.println("未找到文件名 " + fileName + " 对应的业务数据 (流域编码: " + watershedCode + ", 类型: " + type + ")");