From 4506f527607791b2c99c00c27781bebdcf88a0e9 Mon Sep 17 00:00:00 2001 From: wangzhijiang <1358525938@qq.com> Date: Wed, 5 Jun 2024 09:48:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E6=A1=A3=E6=8B=86=E5=88=86=EF=BC=8C?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=9C=A8=E7=BA=BF=E7=BC=96=E8=BE=91docx?= =?UTF-8?q?=E6=96=87=E4=BB=B6document.xml=E5=86=85=E5=AE=B9=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/jero/common/util/FileUtils.java | 164 ------------------ .../split/service/impl/FileSpiltService.java | 20 ++- 2 files changed, 16 insertions(+), 168 deletions(-) diff --git a/byd-structuredplugins-serve/laws-base/laws-base-core/src/main/java/com/jero/common/util/FileUtils.java b/byd-structuredplugins-serve/laws-base/laws-base-core/src/main/java/com/jero/common/util/FileUtils.java index b85af78..2abd3d0 100644 --- a/byd-structuredplugins-serve/laws-base/laws-base-core/src/main/java/com/jero/common/util/FileUtils.java +++ b/byd-structuredplugins-serve/laws-base/laws-base-core/src/main/java/com/jero/common/util/FileUtils.java @@ -12,8 +12,6 @@ import org.slf4j.LoggerFactory; import java.io.*; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; public class FileUtils { @@ -51,167 +49,5 @@ public class FileUtils { e.printStackTrace(); } } - - - /** - * 处理在线编辑文件中,拆分图片,document.xml里面标签问题。 - * @return - */ - public static InputStream docxModifier(InputStream inputStream){ - InputStream resultIs = null; - // 把文件流转成docx文件。存储到本地临时文件 - byte[] buf = new byte[1024 * 8]; - String tempFileName = UUIDUtils.randomUUID20(); - LOGGER.info("处理docx文件中,document.xml文件标签开始,目录名为:" + tempFileName); - String filePath = "D:\\opt\\splitTempFile\\" + tempFileName + "\\"; - String afterFilePath = "D:\\opt\\splitTempFile\\" + tempFileName; - File filePathDir = new File(filePath); - if (!filePathDir.exists()) { - filePathDir.mkdirs(); - } - String tempFileUrl = filePath + tempFileName + ".docx"; - String unZipFileDir = filePath; - try { - FileOutputStream fileOut = new FileOutputStream(new File(tempFileUrl)); - while (true) { - int read = 0; - if (inputStream != null) { - read = inputStream.read(buf); - } - if (read == -1) { - break; - } - fileOut.write(buf, 0, read); - } - // 查看文件获取是否成功 - if (fileOut.getFD().valid() == true) { - LOGGER.info("获取文件保存成功"); - } else { - LOGGER.info("获取文件失败"); - } - fileOut.flush(); - fileOut.close(); - }catch (Exception ex){ - ex.printStackTrace(); - LOGGER.error("文件流转成docx存储到本地失败:" + ex.getMessage()); - } - - // 将docx文件解压。 - unpackDocx(tempFileUrl,unZipFileDir); - // 解压完,临时文件就没用了,删掉。 - deleteFolders(tempFileUrl); - - // 获取document.xml文件,解析该文件。 - String documentXmlUrl = unZipFileDir + "\\word\\document.xml"; - try { - InputStream documentXmlIs = new FileInputStream(new File(documentXmlUrl)); - ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(); - copyStream(documentXmlIs, xmlOutputStream); - String xmlContent = new String(xmlOutputStream.toByteArray(), "UTF-8"); - - // 将标签去掉。 - xmlContent = xmlContent.replace("", "") - .replace("", "") - .replace("", "") - .replace("", ""); - LOGGER.info("去掉标签后的XML内容:" + xmlContent); - // 放回原位。 - // 将之前的document.xml文件删掉,创建一个新的document.xml文件。并把替换后的内容写进去。 - documentXmlIs.close(); - deleteFolders(documentXmlUrl); - - FileWriter xmlFileWriter = new FileWriter(documentXmlUrl); - BufferedWriter bufferedWriter = new BufferedWriter(xmlFileWriter); - bufferedWriter.write(xmlContent); - bufferedWriter.close(); - - }catch (Exception ex){ - ex.printStackTrace(); - LOGGER.error("处理document.xml文件失败:" + ex.getMessage()); - } - // 压缩成docx文件,转成文件流返回。 - compressDocx(afterFilePath, tempFileUrl); - try { - resultIs = new FileInputStream(tempFileUrl); -// resultIs.close(); - } catch (Exception ex) { - ex.printStackTrace(); - LOGGER.error("获取处理完之后的docx失败:" + ex.getMessage()); - } - // 删除创建的临时文件 -// deleteFolders(filePath); - return resultIs; - } - - /** - * 解压docx文件 - * @param docxFilePath docx文件路径 - * @param destDirPath 解压后的文件夹路径 - */ - public static void unpackDocx(String docxFilePath,String destDirPath){ - ZipUtil.unZip(new File(docxFilePath), destDirPath); - } - - // 辅助方法:复制输入流到输出流 - private static void copyStream(InputStream inputStream, OutputStream outputStream) throws IOException { - byte[] buffer = new byte[1024]; - int length; - while ((length = inputStream.read(buffer)) > 0) { - outputStream.write(buffer, 0, length); - } - inputStream.close(); - outputStream.flush(); - } - - - /** - * 压缩docx文件 - * @param folderPath 文件夹路径 - * @param docxFilePath 压缩后的docx文件路径。 - */ - public static void compressDocx(String folderPath, String docxFilePath) { - - try { - File folder = new File(folderPath); - FileOutputStream fos = new FileOutputStream(docxFilePath); - ZipOutputStream zos = new ZipOutputStream(fos); - - addFilesToZip(folder, zos, ""); - - zos.close(); - fos.close(); - } catch (Exception ex){ - ex.printStackTrace(); - LOGGER.error("压缩docx文件失败:" + ex.getMessage()); - } - } - - public static void addFilesToZip(File folder, ZipOutputStream zos, String parentFolderName) throws IOException { - for (File file : folder.listFiles()) { - if (file.isDirectory()) { - addFilesToZip(file, zos, parentFolderName + file.getName() + "/"); - continue; - } - - byte[] buffer = new byte[1024]; - FileInputStream fis = new FileInputStream(file); - zos.putNextEntry(new ZipEntry(parentFolderName + file.getName())); - int length; - while ((length = fis.read(buffer)) > 0) { - zos.write(buffer, 0, length); - } - zos.closeEntry(); - fis.close(); - } - } - - public static void main(String[] args) { - try { -// compressDocx("D:\\opt\\splitTempFile\\GZA688BPVSW4T3Y7YK9K\\","D:\\opt\\splitTempFile\\GZA688BPVSW4T3Y7YK9K\\GZA688BPVSW4T3Y7YK9K.docx"); - compressDocx("D:\\opt\\splitTempFile\\GZA688BPVSW4T3Y7YK9K", "D:\\opt\\splitTempFile\\GZA688BPVSW4T3Y7YK9K\\output.docx"); - } catch (Exception e) { - e.printStackTrace(); - } - } } diff --git a/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java b/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java index a22983a..bb14150 100644 --- a/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java +++ b/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java @@ -237,10 +237,6 @@ public class FileSpiltService { } log.info("下载文档结束========================================================="); } - - // 处理在线编辑文件中,图片拆分问题。 - splitFileIs = com.jero.common.util.FileUtils.docxModifier(splitFileIs); - //记录公式自动编号 MathNumber mathNumber = new MathNumber(); try (InputStream is = splitFileIs;InputStream is1 = splitFileIs1) { @@ -923,6 +919,22 @@ public class FileSpiltService { c.selectPath("./*"); while (c.toNextSelection()) { XmlObject o = c.getObject(); + if(o.getDomNode().getNodeName().equals("mc:AlternateContent")){ + String str = o.toString(); + String imgId = ""; + Pattern pattern = Pattern.compile("r:embed=\"([^\"]+)\""); + Matcher matcher = pattern.matcher(str); + // 查找匹配项 + if (matcher.find()) { + // 提取捕获组中的内容 + String result = matcher.group(1); + imgId = result; + } + if(StringUtils.isNotBlank(imgId)){ + imageBundleList.add(imgId); + continue; + } + } //如果子元素是这样的形式,使用CTDrawing保存图片 if (o instanceof CTDrawing) { CTDrawing drawing = (CTDrawing) o;