提交 cdd549a0 编写于 作者: doc_wei's avatar doc_wei

生成文件完成

上级 f26b154c
package com.skyeye.common.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.security.MessageDigest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
......@@ -487,6 +489,32 @@ public class ToolUtil {
else
return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
}
/**
* 写入内容到文件
* @param content
* @param fileName
* @return
* @throws Exception
*/
public static boolean writeTxtFile(String content, File fileName) throws Exception {
RandomAccessFile mm = null;
boolean flag = false;
FileOutputStream o = null;
try {
o = new FileOutputStream(fileName);
o.write(content.getBytes("GBK"));
o.close();
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (mm != null) {
mm.close();
}
}
return flag;
}
public static void main(String[] args) throws Exception {
......
package com.skyeye.common.dao;
import java.util.List;
import java.util.Map;
public interface CommonDao {
public int insertCodeModelHistory(List<Map<String, Object>> inBeans) throws Exception;
}
......@@ -9,4 +9,6 @@ public interface CommonService {
public void uploadFileBase64(InputObject inputObject, OutputObject outputObject) throws Exception;
public void downloadFileByJsonData(InputObject inputObject, OutputObject outputObject) throws Exception;
}
package com.skyeye.common.service.impl;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import com.skyeye.common.constans.Constants;
import com.skyeye.common.dao.CommonDao;
import com.skyeye.common.object.InputObject;
......@@ -19,6 +32,9 @@ import com.skyeye.common.object.OutputObject;
import com.skyeye.common.service.CommonService;
import com.skyeye.common.util.ToolUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Service
public class CommonServiceImpl implements CommonService{
......@@ -179,5 +195,72 @@ public class CommonServiceImpl implements CommonService{
outputObject.setreturnMessage("上传失败,数据不合法");
}
}
/**
*
* @Title: downloadFileByJsonData
* @Description: 代码生成器下载
* @param @param inputObject
* @param @param outputObject
* @param @throws Exception 参数
* @return void 返回类型
* @throws
*/
@SuppressWarnings({ "static-access" })
@Override
public void downloadFileByJsonData(InputObject inputObject, OutputObject outputObject) throws Exception {
Map<String, Object> map = inputObject.getParams();
JSONArray array = JSONArray.fromObject(map.get("jsonData").toString());
String tPath = inputObject.getRequest().getSession().getServletContext().getRealPath("/");
String basePath = tPath.substring(0, inputObject.getRequest().getSession().getServletContext().getRealPath("/").indexOf(Constants.PROJECT_WEB));
List<Map<String, Object>> inBeans = new ArrayList<>();
Map<String, Object> user = inputObject.getLogParams();
String zipName = ToolUtil.getSurFaceId() + ".zip";
String strZipPath = basePath + "/" + zipName;
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipPath));
byte[] buffer = new byte[1024];
for(int i = 0; i < array.size(); i++){
JSONObject object = (JSONObject) array.get(i);
//加入压缩包
ByteArrayInputStream stream = new ByteArrayInputStream(object.getString("content").getBytes());
out.putNextEntry(new ZipEntry(object.getString("fileName") + "." + object.getString("modelType").toLowerCase()));
int len;
// 读入需要下载的文件的内容,打包到zip文件
while ((len = stream.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
Map<String, Object> bean = new HashMap<>();
bean.put("id", ToolUtil.getSurFaceId());
bean.put("tableName", object.getString("tableName"));
bean.put("groupId", object.getString("groupId"));
bean.put("modelId", object.getString("modelId"));
bean.put("content", object.getString("content"));
bean.put("createId", user.get("id"));
bean.put("filePath", zipName);
bean.put("createTime", ToolUtil.getTimeAndToString());
inBeans.add(bean);
}
out.close();
commonDao.insertCodeModelHistory(inBeans);
// //下载
// //获取输入流
// InputStream bis = new BufferedInputStream(new FileInputStream(new File(strZipPath)));
// inputObject.getResponse().setHeader("REQUESTMATION", "DOWNLOAD");
// // 转码,免得文件名中文乱码
// String filename = URLEncoder.encode(zipName, "UTF-8");
// // 设置文件下载头
// inputObject.getResponse().addHeader("Content-Disposition", "attachment;filename=" + filename);
// // 1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
// inputObject.getResponse().setContentType("multipart/form-data");
// BufferedOutputStream out1 = new BufferedOutputStream(inputObject.getResponse().getOutputStream());
// int len = 0;
// while ((len = bis.read()) != -1) {
// out1.write(len);
// out1.flush();
// }
}
}
......@@ -46,4 +46,20 @@ public class CommonController {
commonService.uploadFileBase64(inputObject, outputObject);
}
/**
*
* @Title: downloadFileByJsonData
* @Description: 代码生成器下载
* @param @param inputObject
* @param @param outputObject
* @param @throws Exception 参数
* @return void 返回类型
* @throws
*/
@RequestMapping("/post/CommonController/downloadFileByJsonData")
@ResponseBody
public void downloadFileByJsonData(InputObject inputObject, OutputObject outputObject) throws Exception{
commonService.downloadFileByJsonData(inputObject, outputObject);
}
}
......@@ -2,6 +2,13 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.skyeye.common.dao.CommonDao">
<insert id="insertCodeModelHistory" parameterType="java.util.Map">
insert into code_model_history
(id, table_name, group_id, model_id, content, file_path, create_id, create_time)
values
<foreach collection="list" item="item" index="index" separator="," >
(#{item.id}, #{item.tableName}, #{item.groupId}, #{item.modelId}, #{item.content}, #{item.filePath}, #{item.createId}, #{item.createTime})
</foreach>
</insert>
</mapper>
\ No newline at end of file
......@@ -337,6 +337,9 @@
<url id="codemodel013" path="/post/CodeModelGroupController/queryCodeModelListByGroupId" val="根据分组id获取模板列表" allUse="1">
<property id="groupId" name="groupId" ref="required" var="分组id"/>
</url>
<url id="codemodel014" path="/post/CommonController/downloadFileByJsonData" val="代码生成器下载" allUse="1">
<property id="jsonData" name="jsonData" ref="required" var="json数据"/>
</url>
<!-- 代码生成器系列结束 -->
</controller>
\ No newline at end of file
......@@ -672,6 +672,7 @@ var AjaxPostUtil = {
// 状态改变的处理
readystatechange: function(xmlhttp) {
var sessionstatus = xmlhttp.getResponseHeader("SESSIONSTATUS");
var requestmation = xmlhttp.getResponseHeader("REQUESTMATION");
if (sessionstatus == "TIMEOUT") {//超时跳转
var win = window;
while (win != win.top){
......@@ -688,8 +689,12 @@ var AjaxPostUtil = {
break;
case "json":
var jsonText = xmlhttp.responseText;
if(jsonText) {
returnValue = eval("(" + jsonText + ")");
if(requestmation == 'DOWNLOAD'){
returnValue = eval('(' + '{"returnMessage":"成功","returnCode":0,"total":0,"rows":"","bean":""}' + ')');
}else{
if(jsonText) {
returnValue = eval("(" + jsonText + ")");
}
}
break;
default:
......
......@@ -5,7 +5,7 @@ layui.config({
version: skyeyeVersion
}).define(['table', 'jquery', 'winui', 'form', 'codemirror', 'xml', 'clike', 'css', 'htmlmixed', 'javascript', 'nginx',
'solr', 'sql', 'vue'], function (exports) {
var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
winui.renderColor();
var $ = layui.$,
......@@ -25,13 +25,27 @@ layui.config({
// groupId:模板所属分组id
// modelName:模板别名
// modelContent:默认内容
// fileName:文件名称
// modelType:模板类型
//}
form.render();
form.on('submit(formSearch)', function (data) {
//表单验证
if (winui.verifyForm(data.elem)) {
var subData = list;
for(var i = 0; i < subData.length; i++){
subData[i].modelContent = "";
}
AjaxPostUtil.request({url:reqBasePath + "codemodel014", params:{jsonData: JSON.stringify(subData)}, type:'json', callback:function(json){
if(json.returnCode == 0){
top.winui.window.msg('保存成功,请前往生成历史下载。', {icon: 1,time: 2000});
parent.layer.close(index);
parent.refreshCode = '0';
}else{
top.winui.window.msg(json.returnMessage, {icon: 2,time: 2000});
}
}});
}
return false;
});
......@@ -125,7 +139,9 @@ layui.config({
tableName: $("#tableName").val(),
groupId: parent.rowId,
modelName: row.modelName,
modelContent: row.modelContent
modelContent: row.modelContent,
fileName: $("#tableZhName").val() + row.modelName,
modelType: row.modelType,
};
insertListIn(list, s);
top.winui.window.msg('转换成功', {icon: 1,time: 2000});
......@@ -165,6 +181,7 @@ layui.config({
list[i].content = replaceModelContent(list[i].modelContent, $("#ControllerPackageName").val(), $("#ServicePackageName").val(),
$("#ServiceImplPackageName").val(), $("#DaoPackageName").val(), $("#tableZhName").val(),
$("#tableFirstISlowerName").val(), $("#tableISlowerName").val(), $("#tableBzName").val());
list[i].fileName = $("#tableZhName").val() + list[i].modelName;
if(list[i].modelId == editId){
textEditor.setOption('readOnly', false);
textEditor.setValue(list[i].content);
......
......@@ -23,3 +23,4 @@ deleteFile(); 删除单个文件
card(); 随机不重复的6-8位
replaceUnderLineAndUpperCase(); 将表名转为Java经常使用的名字,如code_model转CodeModel
toLowerCaseFirstOne(); 字符串首字母转小写
writeTxtFile(); 写入内容到文件
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册