代码下载优化

This commit is contained in:
20932067@zju.edu.cn 2021-02-28 21:20:17 +08:00
parent e26b9feb0a
commit 290036d38c
1 changed files with 99 additions and 50 deletions

View File

@ -1,10 +1,11 @@
package com.hchyun.generator.controller; package com.hchyun.generator.controller;
import java.io.IOException; import java.io.*;
import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.hchyun.generator.aop.Log; import com.hchyun.generator.aop.Log;
@ -55,9 +56,9 @@ public class GenController extends GenBaseController {
@PreAuthorize("@hchyun.hasPermi('tool:gen:list')") @PreAuthorize("@hchyun.hasPermi('tool:gen:list')")
@GetMapping("/list") @GetMapping("/list")
public Serializable genList(GenTable genTable) { public Serializable genList(GenTable genTable) {
startPage(); startPage();
List<GenTable> list = genTableService.selectGenTableList(genTable); List<GenTable> list = genTableService.selectGenTableList(genTable);
return getDataTable(list); return getDataTable(list);
} }
/** /**
@ -66,14 +67,14 @@ public class GenController extends GenBaseController {
@PreAuthorize("@hchyun.hasPermi('tool:gen:query')") @PreAuthorize("@hchyun.hasPermi('tool:gen:query')")
@GetMapping(value = "/{talbleId}") @GetMapping(value = "/{talbleId}")
public AjaxResult getInfo(@PathVariable Long talbleId) { public AjaxResult getInfo(@PathVariable Long talbleId) {
GenTable table = genTableService.selectGenTableById(talbleId); GenTable table = genTableService.selectGenTableById(talbleId);
List<GenTable> tables = genTableService.selectGenTableAll(); List<GenTable> tables = genTableService.selectGenTableAll();
List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(talbleId); List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(talbleId);
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
map.put("info", table); map.put("info" , table);
map.put("rows", list); map.put("rows" , list);
map.put("tables", tables); map.put("tables" , tables);
return AjaxResult.success(map); return AjaxResult.success(map);
} }
/** /**
@ -82,9 +83,9 @@ public class GenController extends GenBaseController {
@PreAuthorize("@hchyun.hasPermi('tool:gen:list')") @PreAuthorize("@hchyun.hasPermi('tool:gen:list')")
@GetMapping("/db/list") @GetMapping("/db/list")
public Serializable dataList(GenTable genTable) { public Serializable dataList(GenTable genTable) {
startPage(); startPage();
List<GenTable> list = genTableService.selectDbTableList(genTable); List<GenTable> list = genTableService.selectDbTableList(genTable);
return getDataTable(list); return getDataTable(list);
} }
/** /**
@ -93,25 +94,25 @@ public class GenController extends GenBaseController {
@PreAuthorize("@hchyun.hasPermi('tool:gen:list')") @PreAuthorize("@hchyun.hasPermi('tool:gen:list')")
@GetMapping(value = "/column/{talbleId}") @GetMapping(value = "/column/{talbleId}")
public Serializable columnList(Long tableId) { public Serializable columnList(Long tableId) {
GenTableDataInfo dataInfo = new GenTableDataInfo(); GenTableDataInfo dataInfo = new GenTableDataInfo();
List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId); List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);
dataInfo.setRows(list); dataInfo.setRows(list);
dataInfo.setTotal(list.size()); dataInfo.setTotal(list.size());
return dataInfo; return dataInfo;
} }
/** /**
* 导入表结构保存 * 导入表结构保存
*/ */
@PreAuthorize("@hchyun.hasPermi('tool:gen:list')") @PreAuthorize("@hchyun.hasPermi('tool:gen:list')")
@Log(title = "代码生成", businessType = GenBusinessType.IMPORT) @Log(title = "代码生成" , businessType = GenBusinessType.IMPORT)
@PostMapping("/importTable") @PostMapping("/importTable")
public AjaxResult importTableSave(String tables) { public AjaxResult importTableSave(String tables) {
String[] tableNames = Convert.toStrArray(tables); String[] tableNames = Convert.toStrArray(tables);
// 查询表信息 // 查询表信息
List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames); List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
genTableService.importGenTable(tableList); genTableService.importGenTable(tableList);
return AjaxResult.success(); return AjaxResult.success();
} }
/** /**
@ -119,19 +120,19 @@ public class GenController extends GenBaseController {
* 修改保存代码生成业务 * 修改保存代码生成业务
*/ */
@PreAuthorize("@hchyun.hasPermi('tool:gen:edit')") @PreAuthorize("@hchyun.hasPermi('tool:gen:edit')")
@Log(title = "代码生成", businessType = GenBusinessType.UPDATE) @Log(title = "代码生成" , businessType = GenBusinessType.UPDATE)
@PutMapping @PutMapping
public AjaxResult editSave(@Validated @RequestBody GenTable genTable) { public AjaxResult editSave(@Validated @RequestBody GenTable genTable) {
genTableService.validateEdit(genTable); genTableService.validateEdit(genTable);
genTableService.updateGenTable(genTable); genTableService.updateGenTable(genTable);
return AjaxResult.success(); return AjaxResult.success();
} }
/** /**
* 删除代码生成 * 删除代码生成
*/ */
@PreAuthorize("@hchyun.hasPermi('tool:gen:remove')") @PreAuthorize("@hchyun.hasPermi('tool:gen:remove')")
@Log(title = "代码生成", businessType = GenBusinessType.DELETE) @Log(title = "代码生成" , businessType = GenBusinessType.DELETE)
@DeleteMapping("/{tableIds}") @DeleteMapping("/{tableIds}")
public AjaxResult remove(@PathVariable Long[] tableIds) { public AjaxResult remove(@PathVariable Long[] tableIds) {
try { try {
@ -153,57 +154,105 @@ public class GenController extends GenBaseController {
@PreAuthorize("@hchyun.hasPermi('tool:gen:preview')") @PreAuthorize("@hchyun.hasPermi('tool:gen:preview')")
@GetMapping("/preview/{tableId}") @GetMapping("/preview/{tableId}")
public AjaxResult preview(@PathVariable("tableId") Long tableId) throws IOException { public AjaxResult preview(@PathVariable("tableId") Long tableId) throws IOException {
Map<String, String> dataMap = genTableService.previewCode(tableId); Map<String, String> dataMap = genTableService.previewCode(tableId);
return AjaxResult.success(dataMap); return AjaxResult.success(dataMap);
} }
/** /**
* 生成代码下载方式 * 生成代码下载方式
*/ */
@PreAuthorize("@hchyun.hasPermi('tool:gen:code')") @PreAuthorize("@hchyun.hasPermi('tool:gen:code')")
@Log(title = "代码生成", businessType = GenBusinessType.GENCODE) @Log(title = "代码生成" , businessType = GenBusinessType.GENCODE)
@GetMapping("/download/{tableName}") @GetMapping("/download/{tableName}")
public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException { public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException {
byte[] data = genTableService.downloadCode(tableName); byte[] data = genTableService.downloadCode(tableName);
genCode(response, data); genCode(response, data);
} }
/** /**
* 生成代码自定义路径 * 生成代码自定义路径
*/ */
@PreAuthorize("@hchyun.hasPermi('tool:gen:code')") @PreAuthorize("@hchyun.hasPermi('tool:gen:code')")
@Log(title = "代码生成", businessType = GenBusinessType.GENCODE) @Log(title = "代码生成" , businessType = GenBusinessType.GENCODE)
@GetMapping("/genCode/{tableName}") @GetMapping("/genCode/{tableName}")
public AjaxResult genCode(@PathVariable("tableName") String tableName) { public AjaxResult genCode(@PathVariable("tableName") String tableName) {
boolean start = genTableService.generatorCode(tableName); boolean start = genTableService.generatorCode(tableName);
if (start) { if (start) {
return AjaxResult.success(); return AjaxResult.success();
} else { } else {
return AjaxResult.error("模板渲染失败!"); return AjaxResult.error("模板渲染失败!");
} }
} }
/** /**
* 同步数据库 * 同步数据库
*/ */
@PreAuthorize("@hchyun.hasPermi('tool:gen:edit')") @PreAuthorize("@hchyun.hasPermi('tool:gen:edit')")
@Log(title = "代码生成", businessType = GenBusinessType.UPDATE) @Log(title = "代码生成" , businessType = GenBusinessType.UPDATE)
@GetMapping("/synchDb/{tableName}") @GetMapping("/synchDb/{tableName}")
public AjaxResult synchDb(@PathVariable("tableName") String tableName) { public AjaxResult synchDb(@PathVariable("tableName") String tableName) {
genTableService.synchDb(tableName); genTableService.synchDb(tableName);
return AjaxResult.success(); return AjaxResult.success();
} }
/** /**
* 批量生成代码 * 批量生成代码
*/ */
@PreAuthorize("@hchyun.hasPermi('tool:gen:code')") @PreAuthorize("@hchyun.hasPermi('tool:gen:code')")
@Log(title = "代码生成", businessType = GenBusinessType.GENCODE) @Log(title = "代码生成" , businessType = GenBusinessType.GENCODE)
@GetMapping("/batchGenCode") @GetMapping("/batchGenCode")
public void batchGenCode(HttpServletResponse response, String tables) throws IOException { public void batchGenCode(HttpServletResponse response, String tables) throws IOException {
String[] tableNames = Convert.toStrArray(tables); String[] tableNames = Convert.toStrArray(tables);
byte[] data = genTableService.downloadCode(tableNames); byte[] data = genTableService.downloadCode(tableNames);
genCode(response, data); 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<String> 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 { private void genCode(HttpServletResponse response, byte[] data) throws IOException {
response.reset(); response.reset();
response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Allow-Origin" , "*");
response.addHeader("Access-Control-Expose-Headers", "Content-Disposition"); response.addHeader("Access-Control-Expose-Headers" , "Content-Disposition");
response.setHeader("Content-Disposition", "attachment; filename=\"hchyun.zip\""); response.setHeader("Content-Disposition" , "attachment; filename=\"hchyun.zip\"");
response.addHeader("Content-Length", "" + data.length); response.addHeader("Content-Length" , "" + data.length);
response.setContentType("application/octet-stream; charset=UTF-8"); response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(data, response.getOutputStream()); IOUtils.write(data, response.getOutputStream());
} }