| | |
| | | package com.ks.codegenerator.controller; |
| | | |
| | | import com.ks.codegenerator.utils.AndroidBuilder; |
| | | import com.ks.codegenerator.utils.ServiceBuilder; |
| | | import com.ks.codegenerator.vo.ServiceVO; |
| | | import org.springframework.stereotype.Controller; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.yeshi.utils.JsonUtil; |
| | | import org.yeshi.utils.StringUtil; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.IOException; |
| | | import java.io.PrintWriter; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.*; |
| | | |
| | | /** |
| | | * @author hxh |
| | |
| | | @Controller |
| | | public class GeneratorController { |
| | | |
| | | @RequestMapping("service") |
| | | public void service(String name, String pks, String subpks, PrintWriter out, HttpServletRequest request) { |
| | | if (StringUtil.isNullOrEmpty(name) || StringUtil.isNullOrEmpty(pks) || StringUtil.isNullOrEmpty(subpks)) { |
| | | out.print(JsonUtil.loadFalseResult("项目名称,项目包名,应用包名不能为空")); |
| | | //生成service |
| | | @RequestMapping("createService") |
| | | public void service(ServiceVO vo, PrintWriter out, HttpServletRequest request) { |
| | | if (StringUtil.isNullOrEmpty(vo.getName()) || StringUtil.isNullOrEmpty(vo.getPks()) || StringUtil.isNullOrEmpty(vo.getSubpks()) || StringUtil.isNullOrEmpty(vo.getChineseName())) { |
| | | out.print(JsonUtil.loadFalseResult("参数不完整")); |
| | | return; |
| | | } |
| | | |
| | | if (StringUtil.isNullOrEmpty(vo.getSqlDataBaseName()) || StringUtil.isNullOrEmpty(vo.getSqlTablePrefix()) || StringUtil.isNullOrEmpty(vo.getMongoDataBaseName())) { |
| | | out.print(JsonUtil.loadFalseResult("参数不完整")); |
| | | return; |
| | | } |
| | | |
| | | |
| | | String rootPath = request.getServletContext().getRealPath("cache"); |
| | | try { |
| | | new ServiceBuilder().setCacheDir(rootPath).setName(name).setPks(pks).setSubpks(subpks).build(); |
| | | |
| | | String path = new ServiceBuilder().setCacheDir(rootPath).setName(vo.getName()).setPks(vo.getPks()).setSubpks(vo.getSubpks()).setProjectChineseName(vo.getChineseName()).setMongoDatabaseName(vo.getMongoDataBaseName()).setSqlDatabaseName(vo.getSqlDataBaseName()).setSqlTablePrefix(vo.getSqlTablePrefix()).build(); |
| | | out.print(JsonUtil.loadTrueResult(path)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | out.print(JsonUtil.loadFalseResult(e.getMessage())); |
| | | } |
| | | } |
| | | |
| | | @RequestMapping("createAndroid") |
| | | public void android(String name, String pks, PrintWriter out, HttpServletRequest request) { |
| | | if (StringUtil.isNullOrEmpty(name) || StringUtil.isNullOrEmpty(pks)) { |
| | | out.print(JsonUtil.loadFalseResult("项目名称,项目包名不能为空")); |
| | | return; |
| | | } |
| | | String rootPath = request.getServletContext().getRealPath("cache"); |
| | | try { |
| | | String path = new AndroidBuilder().setCacheDir(rootPath).setName(name).setPks(pks).build(); |
| | | out.print(JsonUtil.loadTrueResult(path)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | out.print(JsonUtil.loadFalseResult(e.getMessage())); |
| | | } |
| | | } |
| | | |
| | | @RequestMapping("downloadZIP") |
| | | public void downloadServiceZIP(String name, HttpServletResponse response) throws IOException { |
| | | if (!new File(name).exists()) { |
| | | response.sendError(406, "文件已被删除"); |
| | | return; |
| | | } |
| | | response.setHeader("content-disposition", "attachment;filename=" + new File(name).getName()); |
| | | //4.获取要下载的文件输入流 |
| | | InputStream in = new FileInputStream(new File(name).getAbsolutePath()); |
| | | int len = 0; |
| | | //5.创建数据缓冲区 |
| | | byte[] buffer = new byte[1024]; |
| | | //6.通过response对象获取OutputStream流 |
| | | OutputStream out = response.getOutputStream(); |
| | | //7.将FileInputStream流写入到buffer缓冲区 |
| | | while ((len = in.read(buffer)) > 0) { |
| | | //8.使用OutputStream将缓冲区的数据输出到客户端浏览器 |
| | | out.write(buffer, 0, len); |
| | | } |
| | | in.close(); |
| | | } |
| | | |
| | | |