From 98917630154af5664b5d1cc506144fa82d75358c Mon Sep 17 00:00:00 2001 From: timfruit Date: Fri, 12 Mar 2021 00:01:37 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/doc/InfDbDocController.java | 81 ++++++++++++++----- 1 file changed, 63 insertions(+), 18 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/doc/InfDbDocController.java b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/doc/InfDbDocController.java index 0735c4d54..47f748433 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/doc/InfDbDocController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/doc/InfDbDocController.java @@ -1,6 +1,7 @@ package cn.iocoder.dashboard.modules.infra.controller.doc; -import cn.hutool.extra.servlet.ServletUtil; +import cn.hutool.core.lang.UUID; +import cn.iocoder.dashboard.util.servlet.ServletUtils; import cn.smallbun.screw.core.Configuration; import cn.smallbun.screw.core.engine.EngineConfig; import cn.smallbun.screw.core.engine.EngineFileType; @@ -11,17 +12,15 @@ import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import io.swagger.annotations.Api; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; -import org.springframework.http.MediaType; +import org.springframework.util.StreamUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; -import javax.sql.DataSource; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; +import java.io.*; import java.util.Collections; @Api(tags = "数据库文档") @@ -34,36 +33,82 @@ public class InfDbDocController { private static final String FILE_OUTPUT_DIR = System.getProperty("java.io.tmpdir") + File.separator + "db-doc"; - private static final EngineFileType FILE_OUTPUT_TYPE = EngineFileType.HTML; // 可以设置 Word 或者 Markdown 格式 private static final String DOC_FILE_NAME = "数据库文档"; private static final String DOC_VERSION = "1.0.0"; private static final String DOC_DESCRIPTION = "文档描述"; - @Resource - private DataSource dataSource; @GetMapping("/export-html") - public synchronized void exportHtml(HttpServletResponse response) throws FileNotFoundException { + public void exportHtml(@RequestParam(defaultValue = "true") Boolean deleteFile, + HttpServletResponse response) throws IOException { + EngineFileType fileOutputType=EngineFileType.HTML; + doExportFile(fileOutputType,deleteFile,response); + } + + + + @GetMapping("/export-word") + public void exportWord(@RequestParam(defaultValue = "true") Boolean deleteFile, + HttpServletResponse response) throws IOException { + EngineFileType fileOutputType=EngineFileType.WORD; + doExportFile(fileOutputType,deleteFile,response); + } + + + @GetMapping("/export-markdown") + public void exportMarkdown(@RequestParam(defaultValue = "true") Boolean deleteFile, + HttpServletResponse response) throws IOException { + EngineFileType fileOutputType=EngineFileType.MD; + doExportFile(fileOutputType,deleteFile,response); + } + + private void doExportFile(EngineFileType fileOutputType, Boolean deleteFile, + HttpServletResponse response) throws IOException { + String docFileName=DOC_FILE_NAME+"_"+ UUID.fastUUID().toString(true); + String filePath= doExportFile(fileOutputType,docFileName); + String downloadFileName=DOC_FILE_NAME+fileOutputType.getFileSuffix(); //下载后的文件名 + // 读取,返回 + try (InputStream is=new FileInputStream(filePath)){//处理后关闭文件流才能删除 + ServletUtils.writeAttachment(response,downloadFileName, StreamUtils.copyToByteArray(is)); + } + handleDeleteFile(deleteFile,filePath); + } + + + /** + * 输出文件,返回文件路径 + * @param fileOutputType + * @param fileName + * @return + */ + private String doExportFile(EngineFileType fileOutputType, String fileName){ try (HikariDataSource dataSource = buildDataSource()) { // 创建 screw 的配置 Configuration config = Configuration.builder() .version(DOC_VERSION) // 版本 .description(DOC_DESCRIPTION) // 描述 .dataSource(dataSource) // 数据源 - .engineConfig(buildEngineConfig()) // 引擎配置 + .engineConfig(buildEngineConfig(fileOutputType,fileName)) // 引擎配置 .produceConfig(buildProcessConfig()) // 处理配置 .build(); // 执行 screw,生成数据库文档 new DocumentationExecute(config).execute(); - // 读取,返回 - ServletUtil.write(response, - new FileInputStream(FILE_OUTPUT_DIR + File.separator + DOC_FILE_NAME + FILE_OUTPUT_TYPE.getFileSuffix()), - MediaType.TEXT_HTML_VALUE); + + String filePath=FILE_OUTPUT_DIR + File.separator + fileName + fileOutputType.getFileSuffix(); + return filePath; } } + private void handleDeleteFile(Boolean deleteFile,String filePath){ + if(!deleteFile){ + return; + } + File file=new File(filePath); + file.delete(); + } + /** * 创建数据源 */ @@ -83,13 +128,13 @@ public class InfDbDocController { /** * 创建 screw 的引擎配置 */ - private static EngineConfig buildEngineConfig() { + private static EngineConfig buildEngineConfig(EngineFileType fileOutputType,String docFileName) { return EngineConfig.builder() .fileOutputDir(FILE_OUTPUT_DIR) // 生成文件路径 .openOutputDir(false) // 打开目录 - .fileType(FILE_OUTPUT_TYPE) // 文件类型 + .fileType(fileOutputType) // 文件类型 .produceType(EngineTemplateType.freemarker) // 文件类型 - .fileName(DOC_FILE_NAME) // 自定义文件名称 + .fileName(docFileName) // 自定义文件名称 .build(); } From 7089ef7651edb115bedbc504c584a7aedcac98a5 Mon Sep 17 00:00:00 2001 From: timfruit Date: Sat, 13 Mar 2021 13:21:33 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=89=BF=E8=89=BF?= =?UTF-8?q?=E7=9A=84=E8=AF=B4=E6=98=8E=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/doc/InfDbDocController.java | 78 +++++++++++-------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/doc/InfDbDocController.java b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/doc/InfDbDocController.java index 47f748433..c39392c33 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/doc/InfDbDocController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/doc/InfDbDocController.java @@ -1,6 +1,10 @@ package cn.iocoder.dashboard.modules.infra.controller.doc; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.IoUtil; import cn.hutool.core.lang.UUID; +import cn.hutool.core.util.IdUtil; +import cn.hutool.extra.servlet.ServletUtil; import cn.iocoder.dashboard.util.servlet.ServletUtils; import cn.smallbun.screw.core.Configuration; import cn.smallbun.screw.core.engine.EngineConfig; @@ -11,7 +15,11 @@ import cn.smallbun.screw.core.process.ProcessConfig; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.http.MediaType; import org.springframework.util.StreamUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -39,74 +47,78 @@ public class InfDbDocController { @GetMapping("/export-html") + @ApiOperation("导出html格式的数据文档") + @ApiImplicitParams({ + @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class), + }) public void exportHtml(@RequestParam(defaultValue = "true") Boolean deleteFile, - HttpServletResponse response) throws IOException { - EngineFileType fileOutputType=EngineFileType.HTML; - doExportFile(fileOutputType,deleteFile,response); + HttpServletResponse response) throws IOException { + doExportFile(EngineFileType.HTML, deleteFile, response); } - - @GetMapping("/export-word") + @ApiOperation("导出word格式的数据文档") + @ApiImplicitParams({ + @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class), + }) public void exportWord(@RequestParam(defaultValue = "true") Boolean deleteFile, - HttpServletResponse response) throws IOException { - EngineFileType fileOutputType=EngineFileType.WORD; - doExportFile(fileOutputType,deleteFile,response); + HttpServletResponse response) throws IOException { + doExportFile(EngineFileType.WORD, deleteFile, response); } - @GetMapping("/export-markdown") + @ApiOperation("导出markdown格式的数据文档") + @ApiImplicitParams({ + @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class), + }) public void exportMarkdown(@RequestParam(defaultValue = "true") Boolean deleteFile, - HttpServletResponse response) throws IOException { - EngineFileType fileOutputType=EngineFileType.MD; - doExportFile(fileOutputType,deleteFile,response); + HttpServletResponse response) throws IOException { + doExportFile(EngineFileType.MD, deleteFile, response); } private void doExportFile(EngineFileType fileOutputType, Boolean deleteFile, HttpServletResponse response) throws IOException { - String docFileName=DOC_FILE_NAME+"_"+ UUID.fastUUID().toString(true); - String filePath= doExportFile(fileOutputType,docFileName); - String downloadFileName=DOC_FILE_NAME+fileOutputType.getFileSuffix(); //下载后的文件名 + String docFileName = DOC_FILE_NAME + "_" + IdUtil.fastSimpleUUID(); + String filePath = doExportFile(fileOutputType, docFileName); + String downloadFileName = DOC_FILE_NAME + fileOutputType.getFileSuffix(); //下载后的文件名 // 读取,返回 - try (InputStream is=new FileInputStream(filePath)){//处理后关闭文件流才能删除 - ServletUtils.writeAttachment(response,downloadFileName, StreamUtils.copyToByteArray(is)); - } - handleDeleteFile(deleteFile,filePath); + //IoUtil.readBytes 直接读取FileInputStream 不会关闭流,有bug,所以用BufferedInputStream包装一下, 关闭流后才能删除文件 + byte[] content = IoUtil.readBytes(new BufferedInputStream(new FileInputStream(filePath))); + //这里不用hutool工具类,它的中文文件名编码有问题,导致在浏览器下载时有问题 + ServletUtils.writeAttachment(response, downloadFileName, content); + handleDeleteFile(deleteFile, filePath); } - /** * 输出文件,返回文件路径 - * @param fileOutputType - * @param fileName - * @return + * + * @param fileOutputType 文件类型 + * @param fileName 文件名, 无需 ".docx" 等文件后缀 + * @return 生成的文件所在路径 */ - private String doExportFile(EngineFileType fileOutputType, String fileName){ + private String doExportFile(EngineFileType fileOutputType, String fileName) { try (HikariDataSource dataSource = buildDataSource()) { // 创建 screw 的配置 Configuration config = Configuration.builder() .version(DOC_VERSION) // 版本 .description(DOC_DESCRIPTION) // 描述 .dataSource(dataSource) // 数据源 - .engineConfig(buildEngineConfig(fileOutputType,fileName)) // 引擎配置 + .engineConfig(buildEngineConfig(fileOutputType, fileName)) // 引擎配置 .produceConfig(buildProcessConfig()) // 处理配置 .build(); // 执行 screw,生成数据库文档 new DocumentationExecute(config).execute(); - - String filePath=FILE_OUTPUT_DIR + File.separator + fileName + fileOutputType.getFileSuffix(); - return filePath; + return FILE_OUTPUT_DIR + File.separator + fileName + fileOutputType.getFileSuffix(); } } - private void handleDeleteFile(Boolean deleteFile,String filePath){ - if(!deleteFile){ + private void handleDeleteFile(Boolean deleteFile, String filePath) { + if (!deleteFile) { return; } - File file=new File(filePath); - file.delete(); + FileUtil.del(filePath); } /** @@ -128,7 +140,7 @@ public class InfDbDocController { /** * 创建 screw 的引擎配置 */ - private static EngineConfig buildEngineConfig(EngineFileType fileOutputType,String docFileName) { + private static EngineConfig buildEngineConfig(EngineFileType fileOutputType, String docFileName) { return EngineConfig.builder() .fileOutputDir(FILE_OUTPUT_DIR) // 生成文件路径 .openOutputDir(false) // 打开目录 From 562f4cb953bccd1b4be474441e5e36011941b945 Mon Sep 17 00:00:00 2001 From: timfruit Date: Sat, 13 Mar 2021 22:40:16 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=89=BF=E8=89=BF?= =?UTF-8?q?=E7=9A=84=E6=8F=90=E7=A4=BA=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/doc/InfDbDocController.java | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/doc/InfDbDocController.java b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/doc/InfDbDocController.java index c39392c33..f5b293538 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/doc/InfDbDocController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/doc/InfDbDocController.java @@ -48,9 +48,7 @@ public class InfDbDocController { @GetMapping("/export-html") @ApiOperation("导出html格式的数据文档") - @ApiImplicitParams({ - @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class), - }) + @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class) public void exportHtml(@RequestParam(defaultValue = "true") Boolean deleteFile, HttpServletResponse response) throws IOException { doExportFile(EngineFileType.HTML, deleteFile, response); @@ -58,9 +56,7 @@ public class InfDbDocController { @GetMapping("/export-word") @ApiOperation("导出word格式的数据文档") - @ApiImplicitParams({ - @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class), - }) + @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class) public void exportWord(@RequestParam(defaultValue = "true") Boolean deleteFile, HttpServletResponse response) throws IOException { doExportFile(EngineFileType.WORD, deleteFile, response); @@ -68,9 +64,7 @@ public class InfDbDocController { @GetMapping("/export-markdown") @ApiOperation("导出markdown格式的数据文档") - @ApiImplicitParams({ - @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class), - }) + @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class) public void exportMarkdown(@RequestParam(defaultValue = "true") Boolean deleteFile, HttpServletResponse response) throws IOException { doExportFile(EngineFileType.MD, deleteFile, response); @@ -81,12 +75,13 @@ public class InfDbDocController { String docFileName = DOC_FILE_NAME + "_" + IdUtil.fastSimpleUUID(); String filePath = doExportFile(fileOutputType, docFileName); String downloadFileName = DOC_FILE_NAME + fileOutputType.getFileSuffix(); //下载后的文件名 - // 读取,返回 - //IoUtil.readBytes 直接读取FileInputStream 不会关闭流,有bug,所以用BufferedInputStream包装一下, 关闭流后才能删除文件 - byte[] content = IoUtil.readBytes(new BufferedInputStream(new FileInputStream(filePath))); - //这里不用hutool工具类,它的中文文件名编码有问题,导致在浏览器下载时有问题 - ServletUtils.writeAttachment(response, downloadFileName, content); - handleDeleteFile(deleteFile, filePath); + try { + // 读取,返回 + //这里不用hutool工具类,它的中文文件名编码有问题,导致在浏览器下载时有问题 + ServletUtils.writeAttachment(response, downloadFileName, FileUtil.readBytes(filePath)); + }finally { + handleDeleteFile(deleteFile, filePath); + } } /**