From 9afeb3e4b43deb841679c7ef9ea2029d21569889 Mon Sep 17 00:00:00 2001 From: wangzhijiang <1358525938@qq.com> Date: Tue, 4 Jun 2024 10:55:40 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=87=E6=A1=A3=E6=8B=86=E5=88=86?= =?UTF-8?q?=EF=BC=8C=E4=B8=8A=E4=BC=A0=E5=9B=BE=E7=89=87=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=B8=BAlocal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oss/service/impl/OSSFileServiceImpl.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/byd-structuredplugins-serve/laws-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java b/byd-structuredplugins-serve/laws-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java index a274c7d..5d8b98d 100644 --- a/byd-structuredplugins-serve/laws-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java +++ b/byd-structuredplugins-serve/laws-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java @@ -3,6 +3,7 @@ package com.jero.modules.oss.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.jero.common.constant.CommonConstant; import com.jero.common.constant.enums.LanguageEnum; import com.jero.common.exception.JeroBootException; import com.jero.common.system.vo.LoginUser; @@ -35,6 +36,11 @@ public class OSSFileServiceImpl extends ServiceImpl impl private String uploadpath; @Value(value = "${jero.path.uploadCos}") private String uploadCospath; + /** + * 本地:local minio:minio 阿里:alioss + */ + @Value(value = "${jero.uploadType}") + private String uploadType; /** * 文件后缀黑名单 */ @@ -174,21 +180,30 @@ public class OSSFileServiceImpl extends ServiceImpl impl // 将文件上传至腾讯云 cos - String upload = MinioUtil.upload(mf, "/" + uploadCospath); +// String aa = MinioUtil.upload(mf, "/" + uploadCospath); + String upload = ""; + if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)) { + // 本地上传 + upload = CommonUtils.uploadLocal(mf, bizPath, uploadpath); + } else { + // minio上传 + upload = CommonUtils.upload(mf, bizPath, uploadType); + } // LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); String userId = ""; //存入文件表 oSSFile.setFileName(orgName); oSSFile.setId(UuidUtils.getUUID()); + oSSFile.setUrl(upload); oSSFile.setCreateBy(userId); oSSFile.setCreateTime(new Date()); this.save(oSSFile); // 文件路径做特殊处理 // BASE64Encoder base64Encoder = new BASE64Encoder(); // String encode = base64Encoder.encode(filePath.getBytes(StandardCharsets.UTF_8)); - String imgUrl = upload.split("/")[1]; - oSSFile.setUrl(splitUrl + imgUrl); +// String imgUrl = upload.split("/")[1]; + oSSFile.setUrl(splitUrl + oSSFile.getId()); return oSSFile; } From d009e34d2168d5ab4a977f0777df4d96bfe60a11 Mon Sep 17 00:00:00 2001 From: wangzhijiang <1358525938@qq.com> Date: Tue, 4 Jun 2024 17:19:37 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=96=87=E6=A1=A3=E6=8B=86=E5=88=86?= =?UTF-8?q?=EF=BC=8C=E5=A4=84=E7=90=86=E5=9C=A8=E7=BA=BF=E7=BC=96=E8=BE=91?= =?UTF-8?q?docx=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 | 166 +++++++++++++++++- .../split/service/impl/FileSpiltService.java | 4 + 2 files changed, 169 insertions(+), 1 deletion(-) 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 285fdcc..b85af78 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 @@ -9,9 +9,11 @@ package com.jero.common.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; +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 { @@ -49,5 +51,167 @@ 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 6ff45a7..a22983a 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,6 +237,10 @@ public class FileSpiltService { } log.info("下载文档结束========================================================="); } + + // 处理在线编辑文件中,图片拆分问题。 + splitFileIs = com.jero.common.util.FileUtils.docxModifier(splitFileIs); + //记录公式自动编号 MathNumber mathNumber = new MathNumber(); try (InputStream is = splitFileIs;InputStream is1 = splitFileIs1) { 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 3/3] =?UTF-8?q?=E6=96=87=E6=A1=A3=E6=8B=86=E5=88=86?= =?UTF-8?q?=EF=BC=8C=E5=A4=84=E7=90=86=E5=9C=A8=E7=BA=BF=E7=BC=96=E8=BE=91?= =?UTF-8?q?docx=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;