首页 > 编程语言 >java zip

java zip

时间:2023-03-07 17:22:40浏览次数:38  
标签:java String zip templateName tableName item new

@RequestMapping(value="/Table/createCode/{tableName}/{tablePrefix}", method=RequestMethod.GET)
    public void createCode(@PathVariable String tableName, @PathVariable String tablePrefix) throws TemplateException, IOException {        
        String tableShortName = tableName.substring(tableName.indexOf(tablePrefix) + tablePrefix.length() + 1);
        
        cfg = new Configuration(Configuration.VERSION_2_3_32);
        ClassTemplateLoader ctl = new ClassTemplateLoader(getClass(), "/templates");
        cfg.setTemplateLoader(ctl);
        cfg.setDefaultEncoding("UTF-8");
        
        
        root = new HashMap<String, Object>();
        
        List<TableEntity> varList = seService.getTableByName(tableName);
        Iterator<TableEntity> itr = varList.iterator();
        while(itr.hasNext())
        {
            TableEntity item = itr.next();
            if(item.getType().equals("bigint"))
                item.setType("Integer");
            else if(item.getType().equals("character varying"))
                item.setType("String");
        }
        root.put("tableNameUpperCamel", CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableShortName));
        root.put("tableNameLowerCamel", CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tableShortName));
        root.put("tableName", tableName);
        root.put("tableList", varList);
        
        List<String> fileList = new ArrayList<String>();
        fileList.add("entity");
        fileList.add("controller");
        fileList.add("mapper");
        fileList.add("service");
        
        try {
            response.reset();
            response.setContentType("application/zip;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=test.zip");            
            
            ZipOutputStream zip = new ZipOutputStream(response.getOutputStream());
            
            
            for(String file : fileList)
            {
                WriteHttpStream(zip, file, tableShortName);
            }
            
            
            
            zip.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        
        
        
        //format return data    
        //return ReturnUtils.CreateReturn(ReturnUtils.CreateReturnData(varList, varList.size()));
    }
private void WriteHttpStream(ZipOutputStream zip, String templateName, String tableName) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException
    {
        String fileName = tableName + "_" + templateName;
        fileName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, fileName);
        
        zip.putNextEntry(new ZipEntry(templateName + "/"));
        zip.closeEntry();
        
        zip.putNextEntry(new ZipEntry(templateName + "/" + fileName + ".java"));
        Template temp = cfg.getTemplate(templateName + ".ftl");
        Writer out = new OutputStreamWriter(zip);
        temp.process(root, out);
        zip.flush();
        zip.closeEntry();
        
        if(templateName == "mapper")
        {
            zip.putNextEntry(new ZipEntry(templateName + "/" + fileName + ".xml"));
            temp = cfg.getTemplate(templateName + ".xml.ftl");
            out = new OutputStreamWriter(zip);
            temp.process(root, out);
            zip.flush();
            zip.closeEntry();
        }
        
        if(templateName == "service")
        {
            zip.putNextEntry(new ZipEntry(templateName + "/" + fileName + "Impl.java"));
            temp = cfg.getTemplate(templateName + ".impl.ftl");
            out = new OutputStreamWriter(zip);
            temp.process(root, out);
            zip.flush();
            zip.closeEntry();
        }
        
    }

 

package com.accel.UamConfig.entity;

import java.util.LinkedList;
import java.util.List;

public class ${tableNameUpperCamel}Entity {    
<#list tableList as item>
<#assign newName>${item.name?replace("_"," ")?capitalize?uncap_first?replace(" ","")}</#assign>
    private ${item.type} ${newName};
</#list>

<#list tableList as item>
<#assign newName>${item.name?replace("_"," ")?capitalize?uncap_first?replace(" ","")}</#assign>
    public ${item.type} get${newName?capitalize}() {
        return ${newName};
    }
    public void set${newName?capitalize}(${item.type} ${newName}) {
        this.${newName} = ${newName};
    }
</#list>
}

 

标签:java,String,zip,templateName,tableName,item,new
From: https://www.cnblogs.com/tju1895/p/17188835.html

相关文章

  • Java医院电子病历系统源码
    电子病历系统功能总览模板制作、样式控制、数据存储、控件管理、表格管理、图片管理、页面布局、打印设置、病历书写、样式控制、控件输入、病历质控、数据同步、个人模板......
  • Spring Boot | 集成MapStruct实现不同类型Java对象间的自动转换
    MapStruct简介MapStructisacodegeneratorthatgreatlysimplifiestheimplementationofmappingsbetweenJavabeantypesbasedonaconventionoverconfigura......
  • 使用hutool实现压缩zip到输出流(包括空zip)
    //将文件直接压缩至输出流String[]fileNames=fileNameList.toArray(newString[0]);InputStream[]inputStreams=inputStreamList.toArray(newInputStream[0]);i......
  • Java函数(方法)的默认值问题
    Java函数(方法)的默认值问题 Java不能为函数(方法)设置默认参数。原因是“默认参数”和“方法重载”同时支持的话有二义性的问题,但使用“方法重载”可以间接地实现”默认......
  • java流程控制(基础阶段)
    java流程控制(基础阶段)Scanner对象实现程序与人的交互,获取用户的输入。java.util.Scanner基本语法:Scanners=newScanner(System.in);通过Scanner类的nex......
  • NIT-2022JAVA W1-6-1 分数
    题目设计一个表示分数的类Fraction。这个类用两个int类型的变量分别表示分子和分母。注意,在创建和做完运算后应该化简分数为最简形式。如2/4应该被化简为1/2。函数......
  • 部署java项目
    导入数据库:把别的环境的项目可以统一拷贝过来,然后修改配置:程序jar包放在:webapps目录下启动程序:bin目录下执行./startup.sh日志:logs下,tail-fcatalina.out......
  • 生成zip文件,并下载
    1.zip生成/***@paramsourceFilePath:待压缩的文件路径文件的目录,并非文件路径*@paramzipFilePath:压缩后存放路径*@paramfileName......
  • java断言机制(assert)
    java断言机制(assert)概述断言使用的时候不是很多,测试时会使用,springboot中也有使用,总的来说断言还是要慎重。在Java中,同样也有assert关键字,表示断言在Java中,assert关键......
  • 基于ElementUI和Vue.js的SUNBOOK图书后台管理系统(纯HTML、原生Java后端开发)
    一、项目介绍-使用element-ui、axios和Vue.js实现SUNBOOK的页面结构及网页请求-通过JSON传递请求与响应参数-在后端使用JdbcUtilsByDruid实现对数据的增加、删除、......