This commit is contained in:
2025-10-27 09:28:10 +08:00
parent 17bd458302
commit 8e5774955d
5 changed files with 417 additions and 104 deletions

View File

@@ -11,7 +11,6 @@ import com.point.strategy.bean.receive.编码;
import com.point.strategy.classTree.bean.ClassTree;
import com.point.strategy.classTree.mapper.ClassTreeMapper;
import com.point.strategy.common.*;
import com.point.strategy.dao.TentityStructDescriptionMapper;
import com.point.strategy.dao.TtableDescriptionMapper;
import com.point.strategy.dao.TtableStructDescriptionMapper;
import com.point.strategy.docTraditionArrange.docVolume.mapper.DanganguanliMapper;
@@ -35,6 +34,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.ofdrw.tool.merge.OFDMerger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -44,14 +44,14 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sun.management.resources.agent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.*;
@@ -186,7 +186,7 @@ public class FileManageService {
try {
String zipBasePath = tempPath + "\\temp2\\bb\\";
FileUtil.makedir(zipBasePath);
List<String> filePaths = new ArrayList<String>();
Map<String, String> filePaths = new HashMap<>();
String zipName = "批量下载" + DateUtil.date2String(new Date(), 3) + ".zip";
//得到temp_file表数据列表
String myTableName = "";
@@ -201,6 +201,7 @@ public class FileManageService {
List<Map<String, Object>> dataList2 = danganguanliMapper.selectObject(parasMap2);
for (Map<String, Object> tempFile_map : dataList2) {
String file_name_server = StringUtil.formatMap(tempFile_map, "file_name_server");
String file_name = StringUtil.formatMap(tempFile_map, "file_name");
String file_path = StringUtil.formatMap(tempFile_map, "file_path");
String file_des = StringUtil.formatMap(tempFile_map, "file_des");
//如果file_des后面有/就不加/没有就加一个
@@ -209,7 +210,7 @@ public class FileManageService {
}
//String dir = imgUpload + File.separator + file_path + File.separator + file_name_server;
String dir = file_des + file_name_server;
filePaths.add(dir);
filePaths.put(dir, file_name);
}
//打包成zip文件 并下载
DownloadZip.downloadFilesZip(request, response, filePaths, zipBasePath, zipName);
@@ -245,7 +246,7 @@ public class FileManageService {
//批量下载档案复制
String zipBasePath = tempPath + "\\temp2\\bb\\";
FileUtil.makedir(zipBasePath);
List<String> filePaths = new ArrayList<String>();
Map<String, String> filesMap = new HashMap<>();
String zipName = "批量下载" + DateUtil.date2String(new Date(), 3) + ".zip";
//得到temp_file表数据列表
String myTableName = tableName + "_file";
@@ -255,14 +256,16 @@ public class FileManageService {
List<Map<String, Object>> dataList2 = danganguanliMapper.selectObject(parasMap2);
for (Map<String, Object> tempFile_map : dataList2) {
String file_name_server = StringUtil.formatMap(tempFile_map, "file_name_server");
String file_name = StringUtil.formatMap(tempFile_map, "file_name");
String file_path = StringUtil.formatMap(tempFile_map, "file_path");
String file_des = StringUtil.formatMap(tempFile_map, "file_des");
//String dir = imgUpload + File.separator + file_path + File.separator + file_name_server;
String dir = file_des + File.separator + file_name_server;
filePaths.add(dir + file_name_server);
String fileNameInZip = StringUtils.isNotEmpty(file_name) ? file_name : file_name_server;
filesMap.put(dir, fileNameInZip);
}
//打包成zip文件 并下载
DownloadZip.downloadFilesZip(request, response, filePaths, zipBasePath, zipName);
DownloadZip.downloadFilesZip(request, response, filesMap, zipBasePath, zipName);
//删除临时文件夹目录
FileUtil.delAllFile(zipBasePath);
} catch (Exception e) {
@@ -1269,6 +1272,8 @@ public class FileManageService {
HttpServletRequest request,
HttpServletResponse response,
Integer type) {
logger.info("开始合并下载流程文件ID: {}, 表名: {}", fileIds, tableName);
List<File> tempFilesForCleanup = new ArrayList<>();
try {
String relative_path = "uploadFile/" + tableName + "_temp_file/" + fondscode + "/" + id;
String dir = imgUpload + File.separator + relative_path;
@@ -1281,48 +1286,227 @@ public class FileManageService {
}
parasMap2.put("conditionSql", " id in (" + fileIds + " )");
List<Map<String, Object>> dataList2 = danganguanliMapper.selectObject(parasMap2);
if (CollectionUtils.isEmpty(dataList2)) {
logger.warn("合并下载找不到任何文件文件ID: " + fileIds);
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
logger.info("找到 {} 个待处理文件。", dataList2.size());
String temp_pdf = tempPath + "/temp_company_img/";
FileUtil.makedir(temp_pdf);
String[] fileArray = new String[dataList2.size()];
List<String> fileList = new ArrayList<>();
UserRole userRole = userService.getUserRole(request);
String userChnName = userRole.getUser().getUserChnName();
int i = 0;
for (Map<String, Object> tempFile_map : dataList2) {
String file_name_server = StringUtil.formatMap(tempFile_map, "file_name_server");
String file_des = StringUtil.formatMap(tempFile_map, "file_des");
String srcImg = file_des + File.separator + file_name_server;
String srcPath = file_des + File.separator + file_name_server;
logger.info("正在处理文件: {}", srcPath);
String srcpdfPath = destFilePdf(srcImg, userChnName);
String extension = "";
int dotIndex = file_name_server.lastIndexOf('.');
if (dotIndex >= 0) {
extension = file_name_server.substring(dotIndex).toLowerCase();
}
fileArray[i] = srcpdfPath;
i++;
if (extension.equals(".jpg") || extension.equals(".png") || extension.equals(".jpeg")) {
String srcpdfPath = destFilePdf(srcPath, userChnName);
if (srcpdfPath != null) {
logger.info("成功将图片 {} 转换为PDF: {}", srcPath, srcpdfPath);
fileList.add(srcpdfPath);
tempFilesForCleanup.add(new File(srcpdfPath));
tempFilesForCleanup.add(new File(srcpdfPath.replace(".pdf", ".jpg")));
} else {
logger.error("处理并转换图片文件失败: {}", srcPath);
}
} else if (extension.equals(".pdf")) {
logger.info("文件已经是PDF格式将直接合并: {}", srcPath);
fileList.add(srcPath);
} else if (extension.equals(".ofd")) {
logger.info("文件是OFD格式将直接合并: {}", srcPath);
fileList.add(srcPath);
} else {
logger.warn("不支持合并的文件类型: {}. 已跳过此文件。", srcPath);
}
}
String tarFile = temp_pdf + StringUtil.generaterUUID() + ".pdf";
PdfFileHelper.mergePdf(fileArray, tarFile);
String srcFile2 = tarFile;
String tarFile2 = temp_pdf + "setWaterMark-" + DateUtil.date2String(new Date(), 3) + ".pdf";
FileUtil.download(srcFile2, response);
if (fileList.isEmpty()) {
logger.error("没有文件被成功处理。正在中止合并下载。");
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
logger.info("待合并的文件列表: {}", StringUtils.join(fileList, ", "));
boolean hasOfd = fileList.stream().anyMatch(f -> f.toLowerCase().endsWith(".ofd"));
if (hasOfd) {
if (youhongIntegrate) {
logger.info("检测到OFD文件将转换为PDF后合并。");
List<String> pdfFileList = new ArrayList<>();
for (String filePath : fileList) {
if (filePath.toLowerCase().endsWith(".pdf")) {
// 已经是PDF直接添加
pdfFileList.add(filePath);
} else if (filePath.toLowerCase().endsWith(".ofd")) {
// OFD文件转换为PDF
String pdfPath = temp_pdf + StringUtil.generaterUUID() + ".pdf";
try {
OfdToPdfUtil.ofdToPdf(filePath, pdfPath);
pdfFileList.add(pdfPath);
tempFilesForCleanup.add(new File(pdfPath));
logger.info("成功将OFD {} 转换为PDF: {}", filePath, pdfPath);
} catch (Exception e) {
logger.error("OFD转PDF失败: {}", filePath, e);
continue;
}
}
}
if (pdfFileList.isEmpty()) {
logger.error("没有PDF文件可供合并。");
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
// 统一使用PDF合并逻辑
String tarFile = temp_pdf + StringUtil.generaterUUID() + ".pdf";
tempFilesForCleanup.add(new File(tarFile));
logger.info("正在将 {} 个PDF文件合并到 {}", pdfFileList.size(), tarFile);
PdfFileHelper.mergePdf(pdfFileList.toArray(new String[0]), tarFile);
File mergedFile = new File(tarFile);
if (!mergedFile.exists() || mergedFile.length() == 0) {
logger.error("合并后的PDF文件不存在或是空的. 路径: {}", tarFile);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
logger.info("PDF合并完成。合并后文件大小: {} 字节。开始下载: {}", mergedFile.length(), tarFile);
FileUtil.download(tarFile, response);
} else {
logger.error("检测到OFD文件但OFD服务集成未启用无法合并。");
response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
}
} else {
String tarFile = temp_pdf + StringUtil.generaterUUID() + ".pdf";
tempFilesForCleanup.add(new File(tarFile));
logger.info("正在将 {} 个PDF文件合并到 {}", fileList.size(), tarFile);
PdfFileHelper.mergePdf(fileList.toArray(new String[0]), tarFile);
File mergedFile = new File(tarFile);
if (!mergedFile.exists() || mergedFile.length() == 0) {
logger.error("合并后的文件不存在或是空的. 路径: {}", tarFile);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
logger.info("合并完成。合并后文件大小: {} 字节。开始下载: {}", mergedFile.length(), tarFile);
FileUtil.download(tarFile, response);
}
} catch (Exception e) {
logger.error("合并下载过程中发生错误", e);
e.printStackTrace();
} finally {
logger.info("开始清理 {} 个临时文件。", tempFilesForCleanup.size());
for (File tempFile : tempFilesForCleanup) {
if (tempFile != null && tempFile.exists()) {
logger.info("正在删除临时文件: {}", tempFile.getAbsolutePath());
if (!tempFile.delete()) {
logger.warn("删除临时文件失败: {}", tempFile.getAbsolutePath());
}
}
}
}
}
private void downloadOfdFile(String path, HttpServletResponse response) {
InputStream fis = null;
OutputStream toClient = null;
try {
File file = new File(path);
String filename = file.getName();
fis = new BufferedInputStream(new FileInputStream(path));
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
response.addHeader("Content-Length", "" + file.length());
toClient = new BufferedOutputStream(response.getOutputStream());
// 设置OFD文件的Content-Type
response.setContentType("application/ofd");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
toClient.write(buffer, 0, bytesRead);
}
} catch (IOException ex) {
logger.error("下载OFD文件失败", ex);
} finally {
try {
if (fis != null) {
fis.close();
}
if (toClient != null) {
toClient.flush();
toClient.close();
}
} catch (IOException e) {
logger.error("关闭流失败", e);
}
}
}
private String destFilePdf(String srcImg, String userChnName) {
String destImg = srcImg.replaceAll(".jpg", "_waterMark.jpg");
ImgUtil.pressText(
cn.hutool.core.io.FileUtil.file(srcImg), //源图片
cn.hutool.core.io.FileUtil.file(destImg), //目标图片
userChnName + "(内部资料)", //水印文字
Color.red, //水印文字颜色
new Font("黑体", Font.BOLD, 100), //字体
0, //x坐标修正值。 默认在中间,偏移量相对于中间偏移
0, //y坐标修正值。 默认在中间,偏移量相对于中间偏移
0.3f//透明度alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
);
File srcFile = new File(srcImg);
if (!srcFile.exists() || srcFile.length() == 0) {
logger.error("源图片文件不存在或是空的: {}", srcImg);
return null;
}
logger.info("源图片文件 {} 的大小: {} 字节", srcImg, srcFile.length());
int lastDotIndex = srcImg.lastIndexOf('.');
if (lastDotIndex == -1) {
logger.error("无法处理没有扩展名的文件: " + srcImg);
return null;
}
String baseName = srcImg.substring(0, lastDotIndex);
String destImg = baseName + "_waterMark.jpg";
logger.info("正在添加水印: {} -> {}", srcImg, destImg);
try {
ImgUtil.pressText(
cn.hutool.core.io.FileUtil.file(srcImg), //源图片
cn.hutool.core.io.FileUtil.file(destImg), //目标图片
userChnName + "(内部资料)", //水印文字
Color.red, //水印文字颜色
new Font("黑体", Font.BOLD, 100), //字体
0, //x坐标修正值。 默认在中间,偏移量相对于中间偏移
0, //y坐标修正值。 默认在中间,偏移量相对于中间偏移
0.3f//透明度alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
);
} catch (Exception e) {
logger.error("给图片添加水印失败: {}", srcImg, e);
return null;
}
File watermarkedFile = new File(destImg);
if (!watermarkedFile.exists() || watermarkedFile.length() == 0) {
logger.error("带水印的图片未生成或是空的: {}", destImg);
return null;
}
logger.info("带水印的图片文件 {} 的大小: {} 字节", destImg, watermarkedFile.length());
String fromPath = destImg;
String toSource = destImg.replaceAll(".jpg", ".pdf");
String toSource = baseName + "_waterMark.pdf";
logger.info("正在将带水印的图片转换为PDF: {} -> {}", fromPath, toSource);
Img2pdf.jpg2pdfSortedByFilename(fromPath, toSource);
File pdfFile = new File(toSource);
if (!pdfFile.exists() || pdfFile.length() == 0) {
logger.error("转换后的PDF文件未生成或是空的: {}", toSource);
return null;
}
logger.info("转换后的PDF文件 {} 的大小: {} 字节", toSource, pdfFile.length());
return toSource;
}