From f9fae83bbbddfad9983065df1ac91f54a0259bd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E5=BC=BA=E5=BC=BA?= <787203167> Date: Wed, 5 Jun 2024 16:25:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9F=BA=E7=BA=BF=E4=BF=9D?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/OnlyOfficeController.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/onlyoffice/controller/OnlyOfficeController.java b/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/onlyoffice/controller/OnlyOfficeController.java index a118a6b..798d845 100644 --- a/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/onlyoffice/controller/OnlyOfficeController.java +++ b/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/onlyoffice/controller/OnlyOfficeController.java @@ -766,5 +766,58 @@ public class OnlyOfficeController { } } } + + @ApiOperation(value = "|File|在线编辑下载docx文件") + @GetMapping("/onlyOfficeDownloadDocxFileTwo") + public void onlyOfficeDownloadDocxFileTwo(String fileId, HttpServletResponse response, HttpServletRequest request) { + OSSFile ossFile = ossFileService.getById(fileId); + if (ObjectUtils.isNotEmpty(ossFile)) { + InputStream is = null; + OutputStream os = null; + response.reset(); + try { + String fileOldName = fileNameEncoding(ossFile.getFileName(), request); + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); + response.setHeader("Content-Disposition", "attachment;filename=\"" + fileOldName + "\""); + response.setContentType("application/octet-stream"); + + // 加载文档 + Document doc = new Document(ossFile.getUrl()); + doc.getSections().get(0).getPageSetup().setPageNumberStyle(PageNumberStyle.Roman_Upper); + doc.getSections().get(0).getPageSetup().setOrientation(PageOrientation.Portrait);//纵向 + doc.getSections().get(1).getPageSetup().setOrientation(PageOrientation.Portrait);//纵向 + + String downloadDocxFileTempUrl = filePath + "/downloadDocxFileTemp"; + File downloadDocxFileTempUrlFile = new File(downloadDocxFileTempUrl); + + if (!downloadDocxFileTempUrlFile.exists()) { + downloadDocxFileTempUrlFile.mkdirs(); + } + String newFileName = UUID.randomUUID().toString().replace("-", "") + ".docx"; + String newFileUrl = downloadDocxFileTempUrl + "/" + newFileName; + + //保存文档 + doc.saveToFile(newFileUrl); + + File newFile = new File(newFileUrl); + is = new FileInputStream(newFile); + os = response.getOutputStream(); + IOUtils.copy(is, os); + os.flush(); + + is.close(); + if (newFile.delete()) { + logger.info("在线编辑下载-临时文件删除成功: " + newFileUrl); + } else { + logger.info("在线编辑下载-临时文件删除失败: " + newFileUrl); + } + } catch (IOException e) { + logger.error(e.getMessage(), e); + } finally { + IOUtils.closeQuietly(is); + IOUtils.closeQuietly(os); + } + } + } }