From 290036d38c111b879654b8e7bbc644b17ac03738 Mon Sep 17 00:00:00 2001 From: "20932067@zju.edu.cn" Date: Sun, 28 Feb 2021 21:20:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=B8=8B=E8=BD=BD=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generator/controller/GenController.java | 149 ++++++++++++------ 1 file changed, 99 insertions(+), 50 deletions(-) diff --git a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/controller/GenController.java b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/controller/GenController.java index 4ccafde..a73423a 100644 --- a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/controller/GenController.java +++ b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/controller/GenController.java @@ -1,10 +1,11 @@ package com.hchyun.generator.controller; -import java.io.IOException; -import java.io.Serializable; +import java.io.*; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import javax.servlet.http.HttpServletResponse; import com.hchyun.generator.aop.Log; @@ -55,9 +56,9 @@ public class GenController extends GenBaseController { @PreAuthorize("@hchyun.hasPermi('tool:gen:list')") @GetMapping("/list") public Serializable genList(GenTable genTable) { - startPage(); - List list = genTableService.selectGenTableList(genTable); - return getDataTable(list); + startPage(); + List list = genTableService.selectGenTableList(genTable); + return getDataTable(list); } /** @@ -66,14 +67,14 @@ public class GenController extends GenBaseController { @PreAuthorize("@hchyun.hasPermi('tool:gen:query')") @GetMapping(value = "/{talbleId}") public AjaxResult getInfo(@PathVariable Long talbleId) { - GenTable table = genTableService.selectGenTableById(talbleId); - List tables = genTableService.selectGenTableAll(); - List list = genTableColumnService.selectGenTableColumnListByTableId(talbleId); - Map map = new HashMap(); - map.put("info", table); - map.put("rows", list); - map.put("tables", tables); - return AjaxResult.success(map); + GenTable table = genTableService.selectGenTableById(talbleId); + List tables = genTableService.selectGenTableAll(); + List list = genTableColumnService.selectGenTableColumnListByTableId(talbleId); + Map map = new HashMap(); + map.put("info" , table); + map.put("rows" , list); + map.put("tables" , tables); + return AjaxResult.success(map); } /** @@ -82,9 +83,9 @@ public class GenController extends GenBaseController { @PreAuthorize("@hchyun.hasPermi('tool:gen:list')") @GetMapping("/db/list") public Serializable dataList(GenTable genTable) { - startPage(); - List list = genTableService.selectDbTableList(genTable); - return getDataTable(list); + startPage(); + List list = genTableService.selectDbTableList(genTable); + return getDataTable(list); } /** @@ -93,25 +94,25 @@ public class GenController extends GenBaseController { @PreAuthorize("@hchyun.hasPermi('tool:gen:list')") @GetMapping(value = "/column/{talbleId}") public Serializable columnList(Long tableId) { - GenTableDataInfo dataInfo = new GenTableDataInfo(); - List list = genTableColumnService.selectGenTableColumnListByTableId(tableId); - dataInfo.setRows(list); - dataInfo.setTotal(list.size()); - return dataInfo; + GenTableDataInfo dataInfo = new GenTableDataInfo(); + List list = genTableColumnService.selectGenTableColumnListByTableId(tableId); + dataInfo.setRows(list); + dataInfo.setTotal(list.size()); + return dataInfo; } /** * 导入表结构(保存) */ @PreAuthorize("@hchyun.hasPermi('tool:gen:list')") - @Log(title = "代码生成", businessType = GenBusinessType.IMPORT) + @Log(title = "代码生成" , businessType = GenBusinessType.IMPORT) @PostMapping("/importTable") public AjaxResult importTableSave(String tables) { - String[] tableNames = Convert.toStrArray(tables); - // 查询表信息 - List tableList = genTableService.selectDbTableListByNames(tableNames); - genTableService.importGenTable(tableList); - return AjaxResult.success(); + String[] tableNames = Convert.toStrArray(tables); + // 查询表信息 + List tableList = genTableService.selectDbTableListByNames(tableNames); + genTableService.importGenTable(tableList); + return AjaxResult.success(); } /** @@ -119,19 +120,19 @@ public class GenController extends GenBaseController { * 修改保存代码生成业务 */ @PreAuthorize("@hchyun.hasPermi('tool:gen:edit')") - @Log(title = "代码生成", businessType = GenBusinessType.UPDATE) + @Log(title = "代码生成" , businessType = GenBusinessType.UPDATE) @PutMapping public AjaxResult editSave(@Validated @RequestBody GenTable genTable) { - genTableService.validateEdit(genTable); - genTableService.updateGenTable(genTable); - return AjaxResult.success(); + genTableService.validateEdit(genTable); + genTableService.updateGenTable(genTable); + return AjaxResult.success(); } /** * 删除代码生成 */ @PreAuthorize("@hchyun.hasPermi('tool:gen:remove')") - @Log(title = "代码生成", businessType = GenBusinessType.DELETE) + @Log(title = "代码生成" , businessType = GenBusinessType.DELETE) @DeleteMapping("/{tableIds}") public AjaxResult remove(@PathVariable Long[] tableIds) { try { @@ -153,57 +154,105 @@ public class GenController extends GenBaseController { @PreAuthorize("@hchyun.hasPermi('tool:gen:preview')") @GetMapping("/preview/{tableId}") public AjaxResult preview(@PathVariable("tableId") Long tableId) throws IOException { - Map dataMap = genTableService.previewCode(tableId); - return AjaxResult.success(dataMap); + Map dataMap = genTableService.previewCode(tableId); + return AjaxResult.success(dataMap); } /** * 生成代码(下载方式) */ @PreAuthorize("@hchyun.hasPermi('tool:gen:code')") - @Log(title = "代码生成", businessType = GenBusinessType.GENCODE) + @Log(title = "代码生成" , businessType = GenBusinessType.GENCODE) @GetMapping("/download/{tableName}") public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException { byte[] data = genTableService.downloadCode(tableName); + genCode(response, data); } + /** * 生成代码(自定义路径) */ @PreAuthorize("@hchyun.hasPermi('tool:gen:code')") - @Log(title = "代码生成", businessType = GenBusinessType.GENCODE) + @Log(title = "代码生成" , businessType = GenBusinessType.GENCODE) @GetMapping("/genCode/{tableName}") public AjaxResult genCode(@PathVariable("tableName") String tableName) { - boolean start = genTableService.generatorCode(tableName); - if (start) { - return AjaxResult.success(); - } else { - return AjaxResult.error("模板渲染失败!"); - } + boolean start = genTableService.generatorCode(tableName); + if (start) { + return AjaxResult.success(); + } else { + return AjaxResult.error("模板渲染失败!"); + } } /** * 同步数据库 */ @PreAuthorize("@hchyun.hasPermi('tool:gen:edit')") - @Log(title = "代码生成", businessType = GenBusinessType.UPDATE) + @Log(title = "代码生成" , businessType = GenBusinessType.UPDATE) @GetMapping("/synchDb/{tableName}") public AjaxResult synchDb(@PathVariable("tableName") String tableName) { - genTableService.synchDb(tableName); - return AjaxResult.success(); + genTableService.synchDb(tableName); + return AjaxResult.success(); } /** * 批量生成代码 */ @PreAuthorize("@hchyun.hasPermi('tool:gen:code')") - @Log(title = "代码生成", businessType = GenBusinessType.GENCODE) + @Log(title = "代码生成" , businessType = GenBusinessType.GENCODE) @GetMapping("/batchGenCode") public void batchGenCode(HttpServletResponse response, String tables) throws IOException { String[] tableNames = Convert.toStrArray(tables); byte[] data = genTableService.downloadCode(tableNames); genCode(response, data); + compress(data); + } + + /** + * 将bytes流写入到本地 + * @param bytes + */ + public static void compress(byte[] bytes) { + try { + FileOutputStream fileOutputStream = new FileOutputStream("F:\\Work\\表白\\huhyun.zip"); + fileOutputStream.write(bytes); + fileOutputStream.flush(); + fileOutputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * 压缩到文件 + * + * @param sourceFileNames 源文件列表 + * @param zipFileName 目标压缩文件 + */ + public static void compress(List sourceFileNames, String zipFileName) { + byte[] buf = new byte[1024]; + File zipFile = new File(zipFileName); + try { + ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile)); + for (int i = 0; i < sourceFileNames.size(); i++) { + File sourceFile = new File(sourceFileNames.get(i)); + FileInputStream fis = new FileInputStream(sourceFile); + zos.putNextEntry(new ZipEntry(sourceFile.getName())); +// logge.info("压缩当前文件:{}",sourceFile.getName()); + int len; + while ((len = fis.read(buf)) > 0) { + zos.write(buf, 0, len); + } + zos.closeEntry(); + fis.close(); + } + zos.close(); + System.out.println("压缩完成"); + } catch (Exception e) { +// log.error("压缩文件异常:",e); + } } /** @@ -211,10 +260,10 @@ public class GenController extends GenBaseController { */ 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=\"hchyun.zip\""); - response.addHeader("Content-Length", "" + data.length); + response.addHeader("Access-Control-Allow-Origin" , "*"); + response.addHeader("Access-Control-Expose-Headers" , "Content-Disposition"); + response.setHeader("Content-Disposition" , "attachment; filename=\"hchyun.zip\""); + response.addHeader("Content-Length" , "" + data.length); response.setContentType("application/octet-stream; charset=UTF-8"); IOUtils.write(data, response.getOutputStream()); }