|
@@ -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<String, String> 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<String, String> 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<String, String> 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);
|
|
|
- }
|
|
|
}
|