mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2025-01-19 19:50:06 +08:00
代码下载的后端 api 接口,完成~
This commit is contained in:
parent
8cfb82659b
commit
6ca92b0efc
@ -76,17 +76,6 @@ public class GenController extends BaseController {
|
|||||||
return AjaxResult.success();
|
return AjaxResult.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成代码(下载方式)
|
|
||||||
*/
|
|
||||||
@PreAuthorize("@ss.hasPermi('tool:gen:code')")
|
|
||||||
@Log(title = "代码生成", businessType = BusinessType.GENCODE)
|
|
||||||
@GetMapping("/download/{tableName}")
|
|
||||||
public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException {
|
|
||||||
byte[] data = genTableService.downloadCode(tableName);
|
|
||||||
genCode(response, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成代码(自定义路径)
|
* 生成代码(自定义路径)
|
||||||
*/
|
*/
|
||||||
|
@ -127,21 +127,6 @@ public class GenTableServiceImpl implements IGenTableService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成代码(下载方式)
|
|
||||||
*
|
|
||||||
* @param tableName 表名称
|
|
||||||
* @return 数据
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public byte[] downloadCode(String tableName) {
|
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
||||||
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
|
||||||
generatorCode(tableName, zip);
|
|
||||||
IOUtils.closeQuietly(zip);
|
|
||||||
return outputStream.toByteArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 同步数据库
|
* 同步数据库
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cn.iocoder.dashboard.modules.tool.controller.codegen;
|
package cn.iocoder.dashboard.modules.tool.controller.codegen;
|
||||||
|
|
||||||
import cn.hutool.core.io.IoUtil;
|
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.CommonResult;
|
||||||
import cn.iocoder.dashboard.common.pojo.PageResult;
|
import cn.iocoder.dashboard.common.pojo.PageResult;
|
||||||
import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenDetailRespVO;
|
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.ToolCodegenColumnDO;
|
||||||
import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO;
|
import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO;
|
||||||
import cn.iocoder.dashboard.modules.tool.service.codegen.ToolCodegenService;
|
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.Api;
|
||||||
import io.swagger.annotations.ApiImplicitParam;
|
import io.swagger.annotations.ApiImplicitParam;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
@ -21,12 +23,11 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
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 static cn.iocoder.dashboard.common.pojo.CommonResult.success;
|
import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
|
||||||
|
|
||||||
@ -84,58 +85,18 @@ public class ToolCodegenController {
|
|||||||
@ApiOperation("下载生成代码")
|
@ApiOperation("下载生成代码")
|
||||||
@GetMapping("/download")
|
@GetMapping("/download")
|
||||||
@ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class)
|
@ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class)
|
||||||
|
// @PreAuthorize("@ss.hasPermi('tool:gen:code')") todo 权限
|
||||||
public void downloadCodegen(@RequestParam("tableId") Long tableId,
|
public void downloadCodegen(@RequestParam("tableId") Long tableId,
|
||||||
HttpServletResponse response) throws IOException {
|
HttpServletResponse response) throws IOException {
|
||||||
// 生成代码
|
// 生成代码
|
||||||
Map<String, String> codes = codegenService.generationCodes(tableId);
|
Map<String, String> codes = codegenService.generationCodes(tableId);
|
||||||
// 构建压缩包
|
// 构建 zip 包
|
||||||
byte[] data;
|
String[] paths = codes.keySet().toArray(new String[0]);
|
||||||
|
ByteArrayInputStream[] ins = codes.values().stream().map(IoUtil::toUtf8Stream).toArray(ByteArrayInputStream[]::new);
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
ZipUtil.zip(outputStream, paths, ins);
|
||||||
for (Map.Entry<String, String> entry : codes.entrySet()) {
|
// 输出
|
||||||
// zip.putNextEntry(new ZipEntry(entry.getKey()));
|
ServletUtils.writeAttachment(response, "codegen.zip", outputStream.toByteArray());
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user