From a7e5e29bd9771b9cc65cfe09d213c470bbe4166b Mon Sep 17 00:00:00 2001 From: 许鹏程 <1821349743@qq.com> Date: 星期三, 17 七月 2024 18:28:11 +0800 Subject: [PATCH] 添加pom版本号 --- src/main/java/com/product/file/util/FileUtil.java | 412 +++++++++++++++++++++++++++++++++++++--------------------- 1 files changed, 263 insertions(+), 149 deletions(-) diff --git a/src/main/java/com/product/file/util/FileUtil.java b/src/main/java/com/product/file/util/FileUtil.java index d6f9688..1b1e553 100644 --- a/src/main/java/com/product/file/util/FileUtil.java +++ b/src/main/java/com/product/file/util/FileUtil.java @@ -1,11 +1,17 @@ package com.product.file.util; +import cn.hutool.core.lang.UUID; +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.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 org.apache.log4j.Logger; +import lombok.extern.slf4j.Slf4j; import java.io.*; import java.util.List; @@ -21,167 +27,275 @@ * @Author: 6c * @Description: */ +@Slf4j public class FileUtil { - private static Logger logger = Logger.getLogger(FileUtil.class); - 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); - private FileUtil() { - } + 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鏂囦欢鐨勫ご閮ㄤ篃鏄疍0CF11E0锛屾墍浠ラ渶瑕佸垽鏂枃浠跺悗缂� + 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鏂囦欢鐨勫ご閮ㄤ篃鏄疍0CF11E0锛屾墍浠ラ渶瑕佸垽鏂枃浠跺悗缂� + return "xls"; + } else { + return "unknown"; + } + } catch ( + IOException e) { + e.printStackTrace(); + return "unknown"; + } + } - /** - * 鎵归噺鎵撳寘 - * - * @param pathList 鏂囦欢璺緞闆嗗悎 - * @param os 杈撳嚭娴� - * @return zip鏂囦欢淇濆瓨缁濆璺緞 - */ - public static long createZip(List<Map<String, String>> pathList, OutputStream os) { - logger.info("姝e湪鎵撳寘鏂囦欢..."); - try { - long size = 0; - ZipOutputStream out = new ZipOutputStream(os); + private static String bytesToHex(byte[] bytes) { + StringBuilder hex = new StringBuilder(); + for (byte b : bytes) { + hex.append(String.format("%02X", b)); + } + return hex.toString(); + } - String downloadPath; - int symbol; - Map<String, Integer> fileNameCountMap = Maps.newHashMap(); - int showCount = 0; - String fileName; - String head; - String tail; - for (Map<String, String> pathMap : pathList) { - downloadPath = pathMap.get(CmnConst.SOURCE_PATH); - symbol = "1".equals(pathMap.get(CmnConst.ENCRPT_SIGN)) ? -1 : 0; + /** + * 鍚堝苟word鏂囨。 + * + * @param source 婧愭枃浠� + * @param target 鐩爣鏂囦欢 + * @return + */ + public static File mergeFile(File source, File target) throws Exception { + if ("doc".equals(FileUtil.checkDocType(source))) { + source = toDocx(source); + } + boolean delTarget = false; + if ("doc".equals(FileUtil.checkDocType(target))) { + target = toDocx(target); + delTarget = true; + } + 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 { + if (delTarget) { + target.delete(); + } + newDoc.close(); + sub.close(); + } + } catch (IOException e) { + throw e; + } catch (Exception e) { + throw e; + } + } - //鑾峰緱鏂囦欢鍚� - fileName = pathMap.get(CmnConst.REAL_FILE_NAME); - head = fileName.substring(0, fileName.lastIndexOf(".")); - tail = fileName.substring(fileName.lastIndexOf(".") + 1); - showCount = fileNameCountMap.get(fileName) == null ? 0 : fileNameCountMap.get(fileName); - if (showCount > 0) { - fileName = head + "(" + showCount + ")." + tail; - } - fileNameCountMap.put(fileName, ++showCount); - logger.info(String.format("姝e湪鎵撳寘鏂囦欢 %s", fileName)); + 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); - //浠ヨ鏂囨爣棰樹负姣忎釜鏂囦欢鍛藉悕 - FileInputStream fis = new FileInputStream(downloadPath); - out.putNextEntry(new ZipEntry(fileName)); + 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; + } - //鍐欏叆鍘嬬缉鍖� - int len; - byte[] buffer = new byte[1024]; - while ((len = fis.read(buffer)) > 0) { - if (symbol == 1) { - // 鍔犲瘑 - logger.info("姝e湪鍔犲瘑..."); - out.write(encryption(buffer), 0, len); - } else if (symbol == -1) { - // 瑙e瘑 - logger.info("姝e湪瑙e瘑..."); - out.write(decryption(buffer), 0, len); - } else { - // 鍗曠函鐨勫鍒� - out.write(buffer, 0, len); - } - size += len; - } - out.closeEntry(); - fis.close(); - } - out.close(); - out.flush(); - return size; - } catch (Exception e) { - throw new BaseException(FileCode.LOAD_FTP_PROPERTIES_FAIL.getValue(), FileCode.LOAD_FTP_PROPERTIES_FAIL.getText(), e); - } - } + 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(), SaveFormat.DOC); + return tempFile; + } catch (Exception e) { + e.printStackTrace(); + } + return docxFile; + } - /** - * 鎷疯礉鏂囦欢 - * - * @param sourceFile - * @param aimPath - * @param symbol 1锛氬姞瀵嗭紝-1锛氳В瀵嗭紝0锛氫笉鍔� - */ - public static File copyFile(File sourceFile, String aimPath, int symbol) { - if (StringUtils.isEmpty(aimPath)) { - throw new BaseException(FileCode.INVALID_FILE_PATH.getValue(), FileCode.INVALID_FILE_PATH.getText()); - } - File aimFile = new File(aimPath); - File aimDir = aimFile.getParentFile(); - if (!aimDir.exists()) { - aimDir.mkdirs(); - } - try (InputStream is = new FileInputStream(sourceFile); - OutputStream os = new FileOutputStream(aimFile);) { - int len; - byte[] b = new byte[1024]; - while ((len = is.read(b)) > 0) { - if (symbol == 1) { - // 鍔犲瘑 - os.write(encryption(b), 0, len); - } else if (symbol == -1) { - // 瑙e瘑 - os.write(decryption(b), 0, len); - } else { - // 鍗曠函鐨勫鍒� - os.write(b, 0, len); - } - } - os.flush(); - return aimFile; - } catch (IOException e) { - throw new BaseException(FileCode.COPY_FILE_FAIL.getValue(), FileCode.COPY_FILE_FAIL.getText(), e); - } - } + /** + * 鎵归噺鎵撳寘 + * + * @param pathList 鏂囦欢璺緞闆嗗悎 + * @param os 杈撳嚭娴� + * @return zip鏂囦欢淇濆瓨缁濆璺緞 + */ + public static long createZip(List<Map<String, String>> pathList, OutputStream os) { + try { + long size = 0; + ZipOutputStream out = new ZipOutputStream(os); - public static void copyFile(File sourceFile, String aimPath) { - copyFile(sourceFile, aimPath, 0); - } + String downloadPath; + int symbol; + Map<String, Integer> fileNameCountMap = Maps.newHashMap(); + int showCount = 0; + String fileName; + String head; + String tail; + for (Map<String, String> pathMap : pathList) { + downloadPath = pathMap.get(CmnConst.SOURCE_PATH); + symbol = "1".equals(pathMap.get(CmnConst.ENCRPT_SIGN)) ? -1 : 0; - /** - * 鍔犲瘑 - * - * @param bytes - * @return - */ - public static byte[] encryption(byte[] bytes) { - /** - * 璁剧疆涓�涓姞瀵嗙畻娉曪紝瑕佹眰鍙�嗭紝杩樺彲浠ラ�氳繃瑙e瘑寰楀埌鍘熸潵鐨勪笢瑗� - * 鎴戜滑杩欐牱鍋氾細姣忎竴涓瓧鑺傜殑鍊硷紝閮藉姞涓婂畠鐨勭储寮曞彿 - * 鐒跺悗锛屼氦鎹㈢涓�涓瓧鑺傚拰鏈�鍚庝竴涓瓧鑺傜殑浣嶇疆锛屽嵆鍔犲瘑缁撴潫 - */ - for (int i = 0; i < bytes.length; i++) { - bytes[i] = (byte) (bytes[i] + i); - } - byte temp = bytes[0]; - bytes[0] = bytes[bytes.length - 1]; - bytes[bytes.length - 1] = temp; - return bytes; + //鑾峰緱鏂囦欢鍚� + fileName = pathMap.get(CmnConst.REAL_FILE_NAME); + head = fileName.substring(0, fileName.lastIndexOf(".")); + tail = fileName.substring(fileName.lastIndexOf(".") + 1); + showCount = fileNameCountMap.get(fileName) == null ? 0 : fileNameCountMap.get(fileName); + if (showCount > 0) { + fileName = head + "(" + showCount + ")." + tail; + } + fileNameCountMap.put(fileName, ++showCount); - } + //浠ヨ鏂囨爣棰樹负姣忎釜鏂囦欢鍛藉悕 + FileInputStream fis = new FileInputStream(downloadPath); + out.putNextEntry(new ZipEntry(fileName)); - /** - * 瑙e瘑 - * - * @param bytes - * @return - */ - public static byte[] decryption(byte[] bytes) { - /** - * 瑙e瘑绠楁硶锛屾槸鍔犲瘑鐨勯�嗚繃绋� - * 鍏堜氦鎹㈢涓�涓瓧鑺傚拰鏈�鍚庝竴涓瓧鑺傜殑浣嶇疆 - * 鐒跺悗锛屾瘡涓�涓瓧鑺傜殑鍊硷紝閮藉噺鍘诲畠鐨勭储寮曞彿 - */ - byte temp = bytes[0]; - bytes[0] = bytes[bytes.length - 1]; - bytes[bytes.length - 1] = temp; - for (int i = 0; i < bytes.length; i++) { - bytes[i] = (byte) (bytes[i] - i); - } - return bytes; - } + //鍐欏叆鍘嬬缉鍖� + int len; + byte[] buffer = new byte[1024]; + while ((len = fis.read(buffer)) > 0) { + if (symbol == 1) { + // 鍔犲瘑 + out.write(encryption(buffer), 0, len); + } else if (symbol == -1) { + // 瑙e瘑 + out.write(decryption(buffer), 0, len); + } else { + // 鍗曠函鐨勫鍒� + out.write(buffer, 0, len); + } + size += len; + } + out.closeEntry(); + fis.close(); + } + out.close(); + out.flush(); + return size; + } catch (Exception e) { + throw new BaseException(FileCode.LOAD_FTP_PROPERTIES_FAIL.getValue(), FileCode.LOAD_FTP_PROPERTIES_FAIL.getText(), e); + } + } + + /** + * 鎷疯礉鏂囦欢 + * + * @param sourceFile + * @param aimPath + * @param symbol 1锛氬姞瀵嗭紝-1锛氳В瀵嗭紝0锛氫笉鍔� + */ + public static File copyFile(File sourceFile, String aimPath, int symbol) { + if (StringUtils.isEmpty(aimPath)) { + throw new BaseException(FileCode.INVALID_FILE_PATH.getValue(), FileCode.INVALID_FILE_PATH.getText()); + } + File aimFile = new File(aimPath); + File aimDir = aimFile.getParentFile(); + if (!aimDir.exists()) { + aimDir.mkdirs(); + } + try (InputStream is = new FileInputStream(sourceFile); + OutputStream os = new FileOutputStream(aimFile);) { + int len; + byte[] b = new byte[1024]; + while ((len = is.read(b)) > 0) { + if (symbol == 1) { + // 鍔犲瘑 + os.write(encryption(b), 0, len); + } else if (symbol == -1) { + // 瑙e瘑 + os.write(decryption(b), 0, len); + } else { + // 鍗曠函鐨勫鍒� + os.write(b, 0, len); + } + } + os.flush(); + return aimFile; + } catch (IOException e) { + throw new BaseException(FileCode.COPY_FILE_FAIL.getValue(), FileCode.COPY_FILE_FAIL.getText(), e); + } + } + + public static void copyFile(File sourceFile, String aimPath) { + copyFile(sourceFile, aimPath, 0); + } + + /** + * 鍔犲瘑 + * + * @param bytes + * @return + */ + public static byte[] encryption(byte[] bytes) { + /** + * 璁剧疆涓�涓姞瀵嗙畻娉曪紝瑕佹眰鍙�嗭紝杩樺彲浠ラ�氳繃瑙e瘑寰楀埌鍘熸潵鐨勪笢瑗� + * 鎴戜滑杩欐牱鍋氾細姣忎竴涓瓧鑺傜殑鍊硷紝閮藉姞涓婂畠鐨勭储寮曞彿 + * 鐒跺悗锛屼氦鎹㈢涓�涓瓧鑺傚拰鏈�鍚庝竴涓瓧鑺傜殑浣嶇疆锛屽嵆鍔犲瘑缁撴潫 + */ + for (int i = 0; i < bytes.length; i++) { + bytes[i] = (byte) (bytes[i] + i); + } + byte temp = bytes[0]; + bytes[0] = bytes[bytes.length - 1]; + bytes[bytes.length - 1] = temp; + return bytes; + + } + + /** + * 瑙e瘑 + * + * @param bytes + * @return + */ + public static byte[] decryption(byte[] bytes) { + /** + * 瑙e瘑绠楁硶锛屾槸鍔犲瘑鐨勯�嗚繃绋� + * 鍏堜氦鎹㈢涓�涓瓧鑺傚拰鏈�鍚庝竴涓瓧鑺傜殑浣嶇疆 + * 鐒跺悗锛屾瘡涓�涓瓧鑺傜殑鍊硷紝閮藉噺鍘诲畠鐨勭储寮曞彿 + */ + byte temp = bytes[0]; + bytes[0] = bytes[bytes.length - 1]; + bytes[bytes.length - 1] = temp; + for (int i = 0; i < bytes.length; i++) { + bytes[i] = (byte) (bytes[i] - i); + } + return bytes; + } } -- Gitblit v1.9.2