Commit 6ca92b0efcf27f1b8fc72b1ac4624ffebe12bc04

Authored by YunaiV
1 parent 8cfb8265

代码下载的后端 api 接口,完成~

ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java
... ... @@ -77,17 +77,6 @@ public class GenController extends BaseController {
77 77 }
78 78  
79 79 /**
80   - * 生成代码(下载方式)
81   - */
82   - @PreAuthorize("@ss.hasPermi('tool:gen:code')")
83   - @Log(title = "代码生成", businessType = BusinessType.GENCODE)
84   - @GetMapping("/download/{tableName}")
85   - public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException {
86   - byte[] data = genTableService.downloadCode(tableName);
87   - genCode(response, data);
88   - }
89   -
90   - /**
91 80 * 生成代码(自定义路径)
92 81 */
93 82 @PreAuthorize("@ss.hasPermi('tool:gen:code')")
... ...
ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java
... ... @@ -127,21 +127,6 @@ public class GenTableServiceImpl implements IGenTableService {
127 127 }
128 128 }
129 129  
130   - /**
131   - * 生成代码(下载方式)
132   - *
133   - * @param tableName 表名称
134   - * @return 数据
135   - */
136   - @Override
137   - public byte[] downloadCode(String tableName) {
138   - ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
139   - ZipOutputStream zip = new ZipOutputStream(outputStream);
140   - generatorCode(tableName, zip);
141   - IOUtils.closeQuietly(zip);
142   - return outputStream.toByteArray();
143   - }
144   -
145 130  
146 131 /**
147 132 * 同步数据库
... ...
src/main/java/cn/iocoder/dashboard/modules/tool/controller/codegen/ToolCodegenController.java
1 1 package cn.iocoder.dashboard.modules.tool.controller.codegen;
2 2  
3 3 import cn.hutool.core.io.IoUtil;
  4 +import cn.hutool.core.util.ZipUtil;
4 5 import cn.iocoder.dashboard.common.pojo.CommonResult;
5 6 import cn.iocoder.dashboard.common.pojo.PageResult;
6 7 import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenDetailRespVO;
... ... @@ -12,6 +13,7 @@ import cn.iocoder.dashboard.modules.tool.convert.codegen.ToolCodegenConvert;
12 13 import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO;
13 14 import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO;
14 15 import cn.iocoder.dashboard.modules.tool.service.codegen.ToolCodegenService;
  16 +import cn.iocoder.dashboard.util.servlet.ServletUtils;
15 17 import io.swagger.annotations.Api;
16 18 import io.swagger.annotations.ApiImplicitParam;
17 19 import io.swagger.annotations.ApiOperation;
... ... @@ -21,12 +23,11 @@ import org.springframework.web.bind.annotation.*;
21 23 import javax.annotation.Resource;
22 24 import javax.servlet.http.HttpServletResponse;
23 25 import javax.validation.Valid;
  26 +import java.io.ByteArrayInputStream;
24 27 import java.io.ByteArrayOutputStream;
25 28 import java.io.IOException;
26 29 import java.util.List;
27 30 import java.util.Map;
28   -import java.util.zip.ZipEntry;
29   -import java.util.zip.ZipOutputStream;
30 31  
31 32 import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
32 33  
... ... @@ -84,58 +85,18 @@ public class ToolCodegenController {
84 85 @ApiOperation("下载生成代码")
85 86 @GetMapping("/download")
86 87 @ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class)
  88 + // @PreAuthorize("@ss.hasPermi('tool:gen:code')") todo 权限
87 89 public void downloadCodegen(@RequestParam("tableId") Long tableId,
88 90 HttpServletResponse response) throws IOException {
89 91 // 生成代码
90 92 Map<String, String> codes = codegenService.generationCodes(tableId);
91   - // 构建压缩包
92   - byte[] data;
  93 + // 构建 zip 包
  94 + String[] paths = codes.keySet().toArray(new String[0]);
  95 + ByteArrayInputStream[] ins = codes.values().stream().map(IoUtil::toUtf8Stream).toArray(ByteArrayInputStream[]::new);
93 96 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
94   - ZipOutputStream zip = new ZipOutputStream(outputStream);
95   - for (Map.Entry<String, String> entry : codes.entrySet()) {
96   -// zip.putNextEntry(new ZipEntry(entry.getKey()));
97   - zip.putNextEntry(new ZipEntry("123"));
98   -// IoUtil.write(zip, Charset.defaultCharset(), false, entry.getValue());
99   - zip.write(entry.getValue().getBytes());
100   - zip.flush();
101   - zip.closeEntry();
102   - if (true) {
103   - break;
104   - }
105   - }
106   - data = outputStream.toByteArray();
107   - IoUtil.close(zip);
108   -// try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
109   -// ZipOutputStream zip = new ZipOutputStream(outputStream)) {
110   -// for (Map.Entry<String, String> entry : codes.entrySet()) {
111   -//// zip.putNextEntry(new ZipEntry(entry.getKey()));
112   -// zip.putNextEntry(new ZipEntry("123"));
113   -//// IoUtil.write(zip, Charset.defaultCharset(), false, entry.getValue());
114   -// zip.write(entry.getValue().getBytes());
115   -// zip.flush();
116   -// zip.closeEntry();
117   -// if (true) {
118   -// break;
119   -// }
120   -// }
121   -// data = outputStream.toByteArray();
122   -// }
123   - // 返回
124   -// ServletUtils.writeAttachment(response, "yudao.zip", data);
125   - genCode(response, data);
  97 + ZipUtil.zip(outputStream, paths, ins);
  98 + // 输出
  99 + ServletUtils.writeAttachment(response, "codegen.zip", outputStream.toByteArray());
126 100 }
127 101  
128   - /**
129   - * 生成zip文件
130   - */
131   - private void genCode(HttpServletResponse response, byte[] data) throws IOException
132   - {
133   - response.reset();
134   - response.addHeader("Access-Control-Allow-Origin", "*");
135   - response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
136   - response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\"");
137   - response.addHeader("Content-Length", "" + data.length);
138   - response.setContentType("application/octet-stream; charset=UTF-8");
139   - IoUtil.write(response.getOutputStream(), false, data);
140   - }
141 102 }
... ...