代码下载优化

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;
@ -70,9 +71,9 @@ public class GenController extends GenBaseController {
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);
} }
@ -104,7 +105,7 @@ public class GenController extends GenBaseController {
* 导入表结构保存 * 导入表结构保存
*/ */
@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);
@ -119,7 +120,7 @@ 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);
@ -131,7 +132,7 @@ public class GenController extends GenBaseController {
* 删除代码生成 * 删除代码生成
*/ */
@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 {
@ -161,18 +162,20 @@ public class GenController extends GenBaseController {
* 生成代码下载方式 * 生成代码下载方式
*/ */
@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);
@ -187,7 +190,7 @@ 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)
@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);
@ -198,12 +201,58 @@ public class GenController extends GenBaseController {
* 批量生成代码 * 批量生成代码
*/ */
@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());
} }