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.

221 lines
8.4 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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@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++);
}
}
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 HashMap<>();
Map<String, Zong> zongMap = new HashMap<>();
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) {
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);
}
//填充宗
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);
}
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 {
FileUtils.delete(new File(o.getAbsolutePath()));
System.out.println("请删除无效的文件:" + o.getAbsolutePath());
throw new RuntimeException("请删除无效的文件:" + o.getAbsolutePath());
}
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());
}
}