| | |
| | | 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.aspose.words.SaveFormat; |
| | | import com.deepoove.poi.XWPFTemplate; |
| | | import com.deepoove.poi.xwpf.NiceXWPFDocument; |
| | | import com.google.common.collect.Maps; |
| | |
| | | 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; |
| | |
| | | */ |
| | | @Slf4j |
| | | public class FileUtil { |
| | | private static Properties properties; |
| | | |
| | | |
| | | private FileUtil() { |
| | | } |
| | | |
| | | public static String checkDocType(File file) { |
| | | try (FileInputStream fis = new FileInputStream(file)) { |
| | | byte[] bytes = new byte[8]; |
| | | fis.read(bytes, 0, 8); |
| | | |
| | | String hex = bytesToHex(bytes); |
| | | |
| | | if (hex.contains("504B0304") && (file.getName().endsWith(".doc") || file.getName().endsWith(".docx"))) { |
| | | return "docx"; |
| | | } else if (hex.contains("D0CF11E0") && (file.getName().endsWith(".doc") || file.getName().endsWith(".docx"))) { |
| | | //因为doc文件的头部也是D0CF11E0,所以需要判断文件后缀 |
| | | return "doc"; |
| | | } |
| | | //增加xls 和 xlsx的判断 |
| | | else if (hex.contains("504B0304") && (file.getName().endsWith(".xls") || file.getName().endsWith(".xlsx"))) { |
| | | return "xlsx"; |
| | | } else if (hex.contains("D0CF11E0") && (file.getName().endsWith(".xls") || file.getName().endsWith(".xlsx"))) { |
| | | //因为xls文件的头部也是D0CF11E0,所以需要判断文件后缀 |
| | | return "xls"; |
| | | } else { |
| | | return "unknown"; |
| | | } |
| | | } catch ( |
| | | IOException e) { |
| | | e.printStackTrace(); |
| | | return "unknown"; |
| | | } |
| | | } |
| | | |
| | | private static String bytesToHex(byte[] bytes) { |
| | | StringBuilder hex = new StringBuilder(); |
| | | for (byte b : bytes) { |
| | | hex.append(String.format("%02X", b)); |
| | | } |
| | | return hex.toString(); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @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")) { |
| | | if ("doc".equals(FileUtil.checkDocType(source))) { |
| | | source = toDocx(source); |
| | | } |
| | | if (target.getName().endsWith(".doc")) { |
| | | boolean delTarget = false; |
| | | if ("doc".equals(FileUtil.checkDocType(target))) { |
| | | target = toDocx(target); |
| | | delTarget = true; |
| | | } |
| | | XWPFTemplate template = XWPFTemplate.compile(source); |
| | | NiceXWPFDocument xwpfDocument = template.getXWPFDocument(); |
| | |
| | | } catch (IOException e) { |
| | | throw e; |
| | | } finally { |
| | | if (delTarget) { |
| | | target.delete(); |
| | | } |
| | | newDoc.close(); |
| | | sub.close(); |
| | | } |
| | |
| | | } |
| | | |
| | | public static File toDocx(File file) { |
| | | if (!AsposeUtil.getLicense(2)) { |
| | | return 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 (FileInputStream is = new FileInputStream(file);) { |
| | | Document document = new Document(is); |
| | | document.save(tempFile.getAbsolutePath(), com.aspose.words.SaveFormat.DOCX); |
| | | return tempFile; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return docxFile; |
| | | } |
| | | |
| | | public static File toDoc(File file) { |
| | | if (!AsposeUtil.getLicense(2)) { |
| | | return file; |
| | | } |
| | | String docxName = UUID.randomUUID().toString() + ".doc"; |
| | | 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); |
| | | document.save(tempFile.getAbsolutePath(), SaveFormat.DOC); |
| | | return tempFile; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | * @return zip文件保存绝对路径 |
| | | */ |
| | | public static long createZip(List<Map<String, String>> pathList, OutputStream os) { |
| | | log.info("正在打包文件..."); |
| | | try { |
| | | long size = 0; |
| | | ZipOutputStream out = new ZipOutputStream(os); |
| | |
| | | fileName = head + "(" + showCount + ")." + tail; |
| | | } |
| | | fileNameCountMap.put(fileName, ++showCount); |
| | | log.info(String.format("正在打包文件 %s", fileName)); |
| | | |
| | | //以论文标题为每个文件命名 |
| | | FileInputStream fis = new FileInputStream(downloadPath); |
| | |
| | | while ((len = fis.read(buffer)) > 0) { |
| | | if (symbol == 1) { |
| | | // 加密 |
| | | log.info("正在加密..."); |
| | | out.write(encryption(buffer), 0, len); |
| | | } else if (symbol == -1) { |
| | | // 解密 |
| | | log.info("正在解密..."); |
| | | out.write(decryption(buffer), 0, len); |
| | | } else { |
| | | // 单纯的复制 |