zip包,然后自動下載下來

創(chuàng)新互聯(lián)公司主營扎賚諾爾網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都App制作,扎賚諾爾h5微信小程序定制開發(fā)搭建,扎賚諾爾網(wǎng)站營銷推廣歡迎扎賚諾爾等地區(qū)企業(yè)咨詢
1.預先定義好模板
2.界面輸入相關(guān)參數(shù)
3.解析模板生成代碼并下載
最后放出源代碼:
package com.et.controller.system.createcode;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.et.controller.base.BaseController;
import com.et.util.DelAllFile;
import com.et.util.FileDownload;
import com.et.util.FileZip;
import com.et.util.Freemarker;
import com.et.util.PageData;
import com.et.util.PathUtil;
/**
* 類名稱:FreemarkerController
* 創(chuàng)建人:Harries
* 創(chuàng)建時間:2015年1月12日
* @version
*/
@Controller
@RequestMapping(value=”/createCode”)
public class CreateCodeController extends BaseController {
/**
* 生成代碼
*/
@RequestMapping(value=”/proCode”)
public void proCode(HttpServletResponse response) throws Exception{
PageData pd = new PageData();
pd = this.getPageData();
/* ============================================================================================= */
String packageName = pd.getString(“packageName”); //包名 ========1
String objectName = pd.getString(“objectName”); //類名 ========2
String tabletop = pd.getString(“tabletop”); //表前綴 ========3
tabletop = null == tabletop?””:tabletop.toUpperCase(); //表前綴轉(zhuǎn)大寫
String zindext = pd.getString(“zindex”); //屬性總數(shù)
int zindex = 0;
if(null != zindext !””.equals(zindext)){
zindex = Integer.parseInt(zindext);
}
ListString[] fieldList = new ArrayListString[](); //屬性集合 ========4
for(int i=0; i zindex; i++){
fieldList.add(pd.getString(“field”+i).split(“,fh,”)); //屬性放到集合里面
}
MapString,Object root = new HashMapString,Object(); //創(chuàng)建數(shù)據(jù)模型
root.put(“fieldList”, fieldList);
root.put(“packageName”, packageName); //包名
root.put(“objectName”, objectName); //類名
root.put(“objectNameLower”, objectName.toLowerCase()); //類名(全小寫)
root.put(“objectNameUpper”, objectName.toUpperCase()); //類名(全大寫)
root.put(“tabletop”, tabletop); //表前綴
root.put(“nowDate”, new Date()); //當前日期
DelAllFile.delFolder(PathUtil.getClasspath()+”admin/ftl”); //生成代碼前,先清空之前生成的代碼
/* ============================================================================================= */
String filePath = “admin/ftl/code/”; //存放路徑
String ftlPath = “createCode”; //ftl路徑
/*生成controller*/
Freemarker.printFile(“controllerTemplate.ftl”, root, “controller/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName+”Controller.java”, filePath, ftlPath);
/*生成service*/
Freemarker.printFile(“serviceTemplate.ftl”, root, “service/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName+”Service.java”, filePath, ftlPath);
/*生成mybatis xml*/
Freemarker.printFile(“mapperMysqlTemplate.ftl”, root, “mybatis_mysql/”+packageName+”/”+objectName+”Mapper.xml”, filePath, ftlPath);
Freemarker.printFile(“mapperOracleTemplate.ftl”, root, “mybatis_oracle/”+packageName+”/”+objectName+”Mapper.xml”, filePath, ftlPath);
/*生成SQL腳本*/
Freemarker.printFile(“mysql_SQL_Template.ftl”, root, “mysql數(shù)據(jù)庫腳本/”+tabletop+objectName.toUpperCase()+”.sql”, filePath, ftlPath);
Freemarker.printFile(“oracle_SQL_Template.ftl”, root, “oracle數(shù)據(jù)庫腳本/”+tabletop+objectName.toUpperCase()+”.sql”, filePath, ftlPath);
/*生成jsp頁面*/
Freemarker.printFile(“jsp_list_Template.ftl”, root, “jsp/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName.toLowerCase()+”_list.jsp”, filePath, ftlPath);
Freemarker.printFile(“jsp_edit_Template.ftl”, root, “jsp/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName.toLowerCase()+”_edit.jsp”, filePath, ftlPath);
/*生成說明文檔*/
Freemarker.printFile(“docTemplate.ftl”, root, “說明.doc”, filePath, ftlPath);
//this.print(“oracle_SQL_Template.ftl”, root); 控制臺打印
/*生成的全部代碼壓縮成zip文件*/
FileZip.zip(PathUtil.getClasspath()+”admin/ftl/code”, PathUtil.getClasspath()+”admin/ftl/code.zip”);
/*下載代碼*/
FileDownload.fileDownload(response, PathUtil.getClasspath()+”admin/ftl/code.zip”, “code.zip”);
}
}
可以的,我說說大概思路,很簡單,你自己具體實現(xiàn)吧,把代碼寫給你沒意義的:
1.將你這段字符串輸出到一個文件里,用Java類文件的方式命名。
2.調(diào)用外部javac命令將該文件編譯。
3.用類加載器(ClassLoad)動態(tài)加載新的class文件并用Class.forName()注冊該類,然后就可以正常使用了。
上面的每一步都能在baidu中找到實現(xiàn)方法,自己發(fā)揮吧。
Freemarker是一個模板框架。我們可以通過Freemarker進行代碼生成或頁面的靜態(tài)生成。 現(xiàn)在簡單的說一下怎樣使用Freemarker Freemarker的主要生成類
public boolean generate(String templateFileName, Map data,
String fileName) {
try {
//取得模板的位置
String templateFileDir=templateFileName.substring(0, templateFileName.lastIndexOf("/"));
//取得模板的名字
String templateFile=templateFileName.substring(templateFileName.lastIndexOf("/"), templateFileName.length());
//取得生成文件的路徑
String genFileDir=fileName.substring(0, fileName.lastIndexOf("/"));
Template template = ConfigurationHelper.getConfiguration(templateFileDir).getTemplate(templateFile);
File fileDir=new File(genFileDir);
org.apache.commons.io.FileUtils.forceMkdir(fileDir);
File output = new File(fileName);
if(output.exists()){
//如何代碼已存在不重復生成
return false;
}
Writer writer = new FileWriter(output);
template.process(data, writer);
writer.close();
} catch (TemplateException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
代碼中的Map 是模板所需要的數(shù)據(jù),我們可以通過面向?qū)ο竦姆椒ò褦?shù)據(jù)存在模板中public boolean genDaoInterface(String fileName){
DaoModel daoModel=new DaoModel();
//設(shè)置Dao實現(xiàn)類的包名
daoModel.setPackageName(DaoConstant.PACKAGE);
//取得接口名
String className=StringUtils.substringBefore(fileName,".");
//設(shè)置接口名
daoModel.setClassName(className);
MapString, Object data = new HashMapString, Object();
data.put("model", daoModel);
//設(shè)置生成的位置
String filePath=new String("src/"+package2path(DaoConstant.PACKAGE)+"/"+fileName);
//代碼生成
return super.generate(DaoConstant.INTERFACE_TEMPLATE, data, filePath);
}
data.put("model", daoModel);由這句代碼可看出我們將可以在模板中直接調(diào)用這些數(shù)據(jù)package ${model.packageName};
public interface ${model.className} extends BaseHibernateDao {
}
3
推薦
ROSE 生成代碼:首先要在Tools--Java/J2EE--Project Specification里面的classpath里面加入你要生成的代碼的目的路徑。然后選中一個或多個包/類,選擇Tools--Java/J2EE--Generate Code,將目的路徑assign給你要生成代碼的包/類,點OK就可以生成代碼了
這樣生成的代碼屬性可能沒有set/ get 方法,可以按下面的做:
在ROSE的TOOLS工具欄下面有一個OPEIONS...的.
點開以后選擇JAVA,
在TYPE中選擇ATTRIBUTE,
MODEL下面有一個PROPERTYTYPE,可以選擇SIMPLE或者BOUND
那等你生成代碼的時候就會自動增加SET和GET方法了
import?java.io.File;??
import?java.io.FileOutputStream;??
import?java.io.IOException;??
import?com.itextpdf.text.*;??
import?com.itextpdf.text.pdf.PdfWriter;??
public?class?PdfTest??
{??
public?static?void?main(String[]?args)?throws?Exception??
{??
Document?pdfDoc?=?new?Document();??
//?將要生成的?pdf?文件的路徑輸出流??
FileOutputStream?pdfFile?=???
new?FileOutputStream(new?File("F:/study/test/firstPdf.pdf"));??
//?pdf?文件中的一個文字段落??
Paragraph?paragraph?=?new?Paragraph("My?first?PDF?file?with?an?image?...");??
Image?image?=?Image.getInstance("F:/study/test/洛克?李.jpg");??
//?用?Document?對象、File?對象獲得?PdfWriter?輸出流對象??
PdfWriter.getInstance(pdfDoc,?pdfFile);??
pdfDoc.open();??//?打開?Document?文檔??
//?添加一個文字段落、一張圖片??
pdfDoc.add(paragraph);??
pdfDoc.add(image);??
pdfDoc.close();??
}??
}
1.生成java代碼
首先選擇你要生成代碼的類圖,然后單擊菜單欄的Language菜單,選擇Generate java code選項。
會出現(xiàn)上圖所示的對話框,選擇文件輸出目錄,單擊確定就可以了。
2. 生成MySQL語句
首先要在物理數(shù)據(jù)模型視圖下(physical data model)選擇菜單欄的Database菜單下的Generate Database,會出現(xiàn)如下對話框:
選擇輸出目錄,你如果只是生成sql腳本,在Generation type選擇Script generation就可以了。如果你想直接連接數(shù)據(jù)庫,就選擇Direct generation。當然你要用mysql數(shù)據(jù)庫,就必須在創(chuàng)建或者用邏輯或者概念模型生成physical data model時就要把dbms選項選為mysql數(shù)據(jù)庫。
文章題目:生成代碼文本java,文本寫代碼
網(wǎng)頁鏈接:http://chinadenli.net/article3/dsgdjis.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務器、微信公眾號、品牌網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、品牌網(wǎng)站制作、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)