You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
10 KiB
Java

2 years ago
package com.docus.sw.fenpan;
import com.alibaba.excel.util.FileUtils;
import com.docus.sw.Config;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.imaging.ImageInfo;
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.Imaging;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.*;
2 years ago
@Slf4j
public class FenpanService {
public void fenpan(String saveUrl,String readUrl){
try {
FenpanService main = new FenpanService();
Map<String, Zong> zongMap = main.readFile(readUrl);
Map<Zong, List<Plate>> zongListMap = new HashMap<>();
//读取需要转宗的文件夹
Integer i = 0;
for (String key : zongMap.keySet()) {
List<Plate> plateList = new ArrayList<>();
Zong zong = zongMap.get(key);
List<Roll> rollList = zong.getRollList();
Plate plate = new Plate(Integer.parseInt(Config.getParam("plate.size")), "盘" + i++);
for (Roll roll : rollList) {
Boolean put = plate.put(roll);
if (!put) {
//磁盘放不进去了
plateList.add(plate);
plate = new Plate(Integer.parseInt(Config.getParam("plate.size")), "盘" + i++);
plate.put(roll);
2 years ago
}
}
plateList.add(plate);
zongListMap.put(zong, plateList);
}
//copy 文件到新的目录
for (Zong zong : zongListMap.keySet()) {
File saveUrlFile = new File(saveUrl + zong.getName());
if (!saveUrlFile.exists()) {
FileUtils.createDirectory(saveUrlFile);
}
for (Plate plate : zongListMap.get(zong)) {
for (Roll roll : plate.getRollList()) {
copyFolder(new File(roll.getAbsolutePath()), new File(saveUrlFile + "/" + plate.getName() + "/" + roll.getName()));
}
}
}
//输出text
for (Zong zong : zongListMap.keySet()) {
System.out.println(zong.getName());
for (Plate plate : zongListMap.get(zong)) {
System.out.print(plate.getName() + " --- ");
for (Roll roll : plate.getRollList()) {
System.out.print(roll.getName() + ",");
}
System.out.println();
}
}
//转换成 宗。卷,件 。
//放到 宗,盘 下面
//输出目录结构
} catch (IOException e) {
log.error("读取配置文件失败!", e);
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws IOException {
//读取配置文件。
Config.loadConfig();
String saveUrl = Config.getParam("file.save.url");
String readUrl = Config.getParam("file.read.url");
new FenpanService().fenpan(saveUrl,readUrl);
}
public Map<String, Zong> readFile(String readUrl) {
//读取文件夹。
List<Pieces> pieces = new ArrayList<>();
Map<String, Roll> rollMap = new LinkedHashMap<>();
Map<String, Zong> zongMap = new LinkedHashMap<>();
2 years ago
this.findAllDir(readUrl, pieces);
//写入文件
for (Pieces piece : pieces) {
//根据文件类型
if (piece.getFileTypeEnum() == FileTypeEnum.WORD) {
//从word 直接提取图片
} else if (piece.getFileTypeEnum() == FileTypeEnum.PDF) {
// 从pdf 提取图片
} else {
//是图片,直接从图片提取
List<Document> documentList = new ArrayList<>();
File sourceFile = new File(piece.getAbsolutePath());
File[] files = sourceFile.listFiles();
for (File file : files) {
//非图片模式,跳过。
if(!(file.getName().endsWith(".jpg") || file.getName().endsWith(".png")
|| file.getName().endsWith(".jpeg") || file.getName().endsWith(".tif")
|| file.getName().endsWith(".tiff"))){
continue;
}
2 years ago
try {
ImageInfo imageInfo = Imaging.getImageInfo(file);
int height = imageInfo.getHeight();
int width = imageInfo.getWidth();
int physicalHeightDpi = imageInfo.getPhysicalHeightDpi();
Document document = new Document(width, height, physicalHeightDpi);
documentList.add(document);
} catch (IOException e) {
FileUtils.delete(file);
throw new RuntimeException("非图片格式", e);
} catch (ImageReadException e) {
FileUtils.delete(file);
throw new RuntimeException(e);
} catch (IllegalArgumentException e) {
FileUtils.delete(file);
}
}
piece.put(documentList);
}
//填充卷
File file = new File(piece.getAbsolutePath());
File parentFile = file.getParentFile();
String name = parentFile.getName();
// System.out.println(name);
Roll roll = rollMap.get(name);
if (roll == null) {
roll = new Roll(name, parentFile.getAbsolutePath());
}
roll.putPieces(piece);
rollMap.put(name, roll);
String zongName = parentFile.getParentFile().getName();
2 years ago
Zong zong = zongMap.get(zongName);
if (zong == null) {
zong = new Zong(parentFile.getParentFile().getAbsolutePath(), parentFile.getParentFile().getName());
2 years ago
}
// zong.put(rollMap.get(name));
2 years ago
zongMap.put(zongName, zong);
}
for(String zongName: zongMap.keySet()){
Zong zong = zongMap.get(zongName);
File zongfile = new File(zong.getAbsolutePath());
File[] files = zongfile.listFiles();
// 创建自定义比较器
Comparator<File> fileComparator = new Comparator<File>() {
@Override
public int compare(File file1, File file2) {
// 使用正则表达式提取数字部分
int num1 = extractNumber(file1.getName());
int num2 = extractNumber(file2.getName());
// 比较提取的数字部分
return Integer.compare(num1, num2);
}
private int extractNumber(String fileName) {
String numberPart = fileName.replaceAll("[^0-9]", "");
return numberPart.isEmpty() ? 0 : Integer.parseInt(numberPart);
}
};
Arrays.sort(files, fileComparator);
for(File file:files){
Roll roll = rollMap.get(file.getName());
zong.put(roll);
}
}
// //填充宗
// for (String name : rollMap.keySet()) {
// Roll roll = rollMap.get(name);
// File file = new File(roll.getAbsolutePath());
// String zongName = file.getParentFile().getName();
// Zong zong = zongMap.get(zongName);
// if (zong == null) {
// zong = new Zong(file.getParentFile().getAbsolutePath(), file.getParentFile().getName());
// }
// zong.put(rollMap.get(name));
// zongMap.put(zongName, zong);
// }
2 years ago
return zongMap;
}
private void findAllDir(String absolutePath, List<Pieces> allDirectory) {
File sourceFile = new File(absolutePath);
File[] files = sourceFile.listFiles();
for (File o : files) {
if (o.isDirectory()) {
findAllDir(o.getAbsolutePath(), allDirectory);
}
if (o.isFile()) {
//判断是word,pdf,pic
if (o.getName().endsWith(".pdf")) {
Pieces pieces = new Pieces(FileTypeEnum.PDF, o.getAbsolutePath(), o.getName());
allDirectory.add(pieces);
} else if (o.getName().endsWith(".docx") || o.getName().endsWith(".doc")) {
Pieces pieces = new Pieces(FileTypeEnum.WORD, o.getAbsolutePath(), o.getName());
allDirectory.add(pieces);
} else if (o.getName().endsWith(".jpg") || o.getName().endsWith(".png")
|| o.getName().endsWith(".jpeg") || o.getName().endsWith(".tif")
|| o.getName().endsWith(".tiff")) {
Pieces pieces = new Pieces(FileTypeEnum.JPG, o.getParentFile().getAbsolutePath(), o.getParentFile().getName());
allDirectory.add(pieces);
} else {
2 years ago
System.out.println("请删除无效的文件:" + o.getAbsolutePath());
2 years ago
}
break;
}
}
}
public static void copyFolder(File sourceFolder, File destinationFolder) throws IOException {
if (!destinationFolder.exists()) {
destinationFolder.mkdirs();
}
for (File file : sourceFolder.listFiles()) {
if (file.isDirectory()) {
copyFolder(file, newFile(destinationFolder, file));
} else {
Path sourcePath = file.toPath();
Path destinationPath = newFile(destinationFolder, file).toPath();
Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
}
}
}
public static File newFile(File parentDir, File childFile) {
return new File(parentDir, childFile.getName());
}
}