diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java index d18e5ac0b..b7be10d62 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java @@ -76,17 +76,6 @@ public class GenController extends BaseController { return AjaxResult.success(); } - /** - * 生成代码(下载方式) - */ - @PreAuthorize("@ss.hasPermi('tool:gen:code')") - @Log(title = "代码生成", businessType = BusinessType.GENCODE) - @GetMapping("/download/{tableName}") - public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException { - byte[] data = genTableService.downloadCode(tableName); - genCode(response, data); - } - /** * 生成代码(自定义路径) */ diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java index e28a352a2..daa574de5 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java @@ -127,21 +127,6 @@ public class GenTableServiceImpl implements IGenTableService { } } - /** - * 生成代码(下载方式) - * - * @param tableName 表名称 - * @return 数据 - */ - @Override - public byte[] downloadCode(String tableName) { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - ZipOutputStream zip = new ZipOutputStream(outputStream); - generatorCode(tableName, zip); - IOUtils.closeQuietly(zip); - return outputStream.toByteArray(); - } - /** * 同步数据库 diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/controller/codegen/ToolCodegenController.java b/src/main/java/cn/iocoder/dashboard/modules/tool/controller/codegen/ToolCodegenController.java index 72b0d329e..fb87c6116 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/controller/codegen/ToolCodegenController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/controller/codegen/ToolCodegenController.java @@ -1,6 +1,7 @@ package cn.iocoder.dashboard.modules.tool.controller.codegen; import cn.hutool.core.io.IoUtil; +import cn.hutool.core.util.ZipUtil; import cn.iocoder.dashboard.common.pojo.CommonResult; import cn.iocoder.dashboard.common.pojo.PageResult; import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenDetailRespVO; @@ -12,6 +13,7 @@ import cn.iocoder.dashboard.modules.tool.convert.codegen.ToolCodegenConvert; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; import cn.iocoder.dashboard.modules.tool.service.codegen.ToolCodegenService; +import cn.iocoder.dashboard.util.servlet.ServletUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; @@ -21,12 +23,11 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; import java.util.Map; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; import static cn.iocoder.dashboard.common.pojo.CommonResult.success; @@ -84,58 +85,18 @@ public class ToolCodegenController { @ApiOperation("下载生成代码") @GetMapping("/download") @ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class) + // @PreAuthorize("@ss.hasPermi('tool:gen:code')") todo 权限 public void downloadCodegen(@RequestParam("tableId") Long tableId, HttpServletResponse response) throws IOException { // 生成代码 Map codes = codegenService.generationCodes(tableId); - // 构建压缩包 - byte[] data; + // 构建 zip 包 + String[] paths = codes.keySet().toArray(new String[0]); + ByteArrayInputStream[] ins = codes.values().stream().map(IoUtil::toUtf8Stream).toArray(ByteArrayInputStream[]::new); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - ZipOutputStream zip = new ZipOutputStream(outputStream); - for (Map.Entry entry : codes.entrySet()) { -// zip.putNextEntry(new ZipEntry(entry.getKey())); - zip.putNextEntry(new ZipEntry("123")); -// IoUtil.write(zip, Charset.defaultCharset(), false, entry.getValue()); - zip.write(entry.getValue().getBytes()); - zip.flush(); - zip.closeEntry(); - if (true) { - break; - } - } - data = outputStream.toByteArray(); - IoUtil.close(zip); -// try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); -// ZipOutputStream zip = new ZipOutputStream(outputStream)) { -// for (Map.Entry entry : codes.entrySet()) { -//// zip.putNextEntry(new ZipEntry(entry.getKey())); -// zip.putNextEntry(new ZipEntry("123")); -//// IoUtil.write(zip, Charset.defaultCharset(), false, entry.getValue()); -// zip.write(entry.getValue().getBytes()); -// zip.flush(); -// zip.closeEntry(); -// if (true) { -// break; -// } -// } -// data = outputStream.toByteArray(); -// } - // 返回 -// ServletUtils.writeAttachment(response, "yudao.zip", data); - genCode(response, data); + ZipUtil.zip(outputStream, paths, ins); + // 输出 + ServletUtils.writeAttachment(response, "codegen.zip", outputStream.toByteArray()); } - /** - * 生成zip文件 - */ - private void genCode(HttpServletResponse response, byte[] data) throws IOException - { - response.reset(); - response.addHeader("Access-Control-Allow-Origin", "*"); - response.addHeader("Access-Control-Expose-Headers", "Content-Disposition"); - response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\""); - response.addHeader("Content-Length", "" + data.length); - response.setContentType("application/octet-stream; charset=UTF-8"); - IoUtil.write(response.getOutputStream(), false, data); - } }