| | |
| | | package com.product.file.util; |
| | | |
| | | import cn.hutool.core.lang.UUID; |
| | | import cn.hutool.core.util.IdUtil; |
| | | import cn.hutool.poi.word.WordUtil; |
| | | import com.aspose.words.Document; |
| | | import com.deepoove.poi.XWPFTemplate; |
| | | import com.deepoove.poi.xwpf.NiceXWPFDocument; |
| | | import com.google.common.collect.Maps; |
| | | import com.product.common.lang.StringUtils; |
| | | import com.product.core.config.Global; |
| | | import com.product.core.exception.BaseException; |
| | | import com.product.file.config.CmnConst; |
| | | import com.product.file.config.FileCode; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.text.WordUtils; |
| | | import org.apache.log4j.Logger; |
| | | import org.apache.poi.openxml4j.opc.OPCPackage; |
| | | import org.apache.poi.xwpf.usermodel.XWPFDocument; |
| | | |
| | | import java.io.*; |
| | | import java.util.List; |
| | |
| | | * @Author: 6c |
| | | * @Description: |
| | | */ |
| | | @Slf4j |
| | | public class FileUtil { |
| | | private static Logger logger = Logger.getLogger(FileUtil.class); |
| | | private static Properties properties; |
| | | |
| | | |
| | | |
| | | private FileUtil() { |
| | | } |
| | | |
| | | /** |
| | | * 合并word文档 |
| | | * |
| | | * @param source 源文件 |
| | | * @param target 目标文件 |
| | | * @return |
| | | */ |
| | | public static File mergeFile(File source, File target) throws Exception { |
| | | //判断文件是否为 docx或doc格式 |
| | | if (!source.getName().endsWith(".docx") && !source.getName().endsWith(".doc")) { |
| | | throw new BaseException(FileCode.ADD_FILE_NOT_ALLOWED); |
| | | } |
| | | //如果是doc格式则转换为docx格式 |
| | | if (source.getName().endsWith(".doc")) { |
| | | source = toDocx(source); |
| | | } |
| | | if (target.getName().endsWith(".doc")) { |
| | | target = toDocx(target); |
| | | } |
| | | XWPFTemplate template = XWPFTemplate.compile(source); |
| | | NiceXWPFDocument xwpfDocument = template.getXWPFDocument(); |
| | | try { |
| | | NiceXWPFDocument sub = new NiceXWPFDocument(new FileInputStream((target))); |
| | | NiceXWPFDocument newDoc = xwpfDocument.merge(sub); |
| | | String tempFile = Global.getSystemConfig("temp.dir", "./attachment/temp") + File.separator + UUID.randomUUID() + ".docx"; |
| | | try (FileOutputStream out = new FileOutputStream(tempFile)) { |
| | | newDoc.write(out); |
| | | newDoc.close(); |
| | | return new File(tempFile); |
| | | } catch (IOException e) { |
| | | throw e; |
| | | } finally { |
| | | newDoc.close(); |
| | | sub.close(); |
| | | } |
| | | } catch (IOException e) { |
| | | throw e; |
| | | } catch (Exception e) { |
| | | throw e; |
| | | } |
| | | } |
| | | |
| | | public static File toDocx(File file) { |
| | | String docxName = UUID.randomUUID().toString() + ".docx"; |
| | | File docxFile = new File(file.getParent() + File.separator + docxName); |
| | | String tempDir = Global.getSystemConfig("temp.dir", "./attachment/temp"); |
| | | File tempFile = new File(tempDir + File.separator + docxName); |
| | | try { |
| | | Document document = new Document(new FileInputStream(file)); |
| | | document.save(tempFile.getAbsolutePath(), com.aspose.words.SaveFormat.DOCX); |
| | | return tempFile; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return docxFile; |
| | | } |
| | | |
| | | /** |
| | | * 批量打包 |
| | |
| | | * @return zip文件保存绝对路径 |
| | | */ |
| | | public static long createZip(List<Map<String, String>> pathList, OutputStream os) { |
| | | logger.info("正在打包文件..."); |
| | | log.info("正在打包文件..."); |
| | | try { |
| | | long size = 0; |
| | | ZipOutputStream out = new ZipOutputStream(os); |
| | |
| | | fileName = head + "(" + showCount + ")." + tail; |
| | | } |
| | | fileNameCountMap.put(fileName, ++showCount); |
| | | logger.info(String.format("正在打包文件 %s", fileName)); |
| | | log.info(String.format("正在打包文件 %s", fileName)); |
| | | |
| | | //以论文标题为每个文件命名 |
| | | FileInputStream fis = new FileInputStream(downloadPath); |
| | |
| | | while ((len = fis.read(buffer)) > 0) { |
| | | if (symbol == 1) { |
| | | // 加密 |
| | | logger.info("正在加密..."); |
| | | log.info("正在加密..."); |
| | | out.write(encryption(buffer), 0, len); |
| | | } else if (symbol == -1) { |
| | | // 解密 |
| | | logger.info("正在解密..."); |
| | | log.info("正在解密..."); |
| | | out.write(decryption(buffer), 0, len); |
| | | } else { |
| | | // 单纯的复制 |