发布模板的方法——替换js引用、清除点击事件、复制文件等

方法代码:

public Map<String, Object> publish(String templateId, String biType, String token) 
	{
		LocalUser localUser=UserUtil.getLocalUser(token);
		User user = userMapper.getUserByNmaeOrPhone(localUser.getUserName());
		UserTemp userTemp = userTempMapper.selectByPrimaryKey(Long.parseLong(templateId));
		List<UserBlock> blocks = userBlockMapper.selectByUserTempId(Long.parseLong(templateId));
		String frontPath = ConfigUtil.getValue("front.path");
		String sourcePath = frontPath + "/" + userTemp.getPath();
		sourcePath = sourcePath.substring(0, sourcePath.lastIndexOf("/"));
		String sourceName = sourcePath.substring(sourcePath.lastIndexOf("/") + 1);
		String target = frontPath + "/views/released";
		
		FileUtil.copyFolder(sourcePath, target);//copy模板文件夹
		
		StringBuffer prefix = new StringBuffer("var obj = {\"main_title\":\"" + userTemp.getTittle() + "\", \"datas\":[");
		for(int i = 0; i < blocks.size(); i++)
		{
			if(i > 0)
			{
				prefix.append(",");
			}
			prefix.append(blocks.get(i).getValuemap());
		}
		
		prefix.append("]}");
		
		String sf = frontPath + "/views/released/released.js";
		String targetName = System.currentTimeMillis() + "";
		String tf = frontPath + "/views/released/" + sourceName + "/js/js_" + targetName + ".js";
		Map<String, String> replaceTxt = new HashMap<String, String>();
		FileUtil.copyFile(sf, tf, replaceTxt, prefix.toString());//生成JS文件
		
		sf = frontPath + "/views/released/" + sourceName + "/index.html";
		tf = frontPath + "/views/released/" + sourceName + "/index_" + targetName + ".html";
		replaceTxt.put("\\s*on(dbl)?click[^\\)]+\\);?\"", "");
		replaceTxt.put("\"js/index.js\"", "\"js/js_" + targetName + ".js\"");
		FileUtil.copyFile(sf, tf, replaceTxt, "");//生成HTML文件,并替换js引用、清除编辑功能点击事件
		
		ReleasedTemp temp = new ReleasedTemp();
		temp.setId(Uuid.getUUID());
		temp.setUserId(Long.parseLong(user.getUid()));//登录者ID
		temp.setBiTemplateImg(userTemp.getField1());
		temp.setBiType(Integer.parseInt(biType));
		temp.setPath(sourceName + "/index_" + targetName + ".html");
		temp.setTitle(userTemp.getTittle());
		
		releasedTempMapper.insert(temp);
		

		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, ConstantUtil.ADD_SUCCESS_MSG, "");
	}

方法中用到的工具类:

package com.sunyard.ec.base.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

public class FileUtil {
    
	/**
	 * 复制自己的文件
	 * @param source
	 * @param name
	 */
	public static void copyFile(String source, String name)
	{
		try 
		{
			File sf = new File(ConfigUtil.getValue("template.source.path") + source);
			byte[] temp = new byte[(int)sf.length()];
			FileInputStream fis = new FileInputStream(sf);
			FileOutputStream fos = new FileOutputStream(ConfigUtil.getValue("template.source.path") + name);
			fis.read(temp);
			fos.write(temp);
			fos.flush();
            fos.close();
            fis.close();
		} 
		catch (Exception e)
		{
            e.printStackTrace();
		}
	}
	
	/**
	 * 复制文件
	 * @param source
	 *        源文件
	 * @param target
	 *        目标文件
	 * @param replaceTxt
	 *        内容替换集合
	 * @param prefix
	 *        前缀
	 */
	public static void copyFile(String source, String target, Map<String, String> replaceTxt, String prefix)
	{
		try 
		{
			File sourceFile = new File(source);
			
			FileReader fr = new FileReader(sourceFile);
			BufferedReader br = new BufferedReader(fr);
			FileOutputStream fos = new FileOutputStream(target);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			
			osw.write(prefix + "\r");
			
			String s = br.readLine();
			while(null != s)
			{
				for(String key : replaceTxt.keySet())
				{
					s = s.replaceAll(key, replaceTxt.get(key));
				}
				
				osw.write(s + "\r");
				s = br.readLine();
			}
			osw.flush();
			osw.close();
			br.close();
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * 复制文件夹
	 * @param source
	 *        源文件夹
	 * @param target
	 *        目标地址
	 */
	public static void copyFolder(String source, String target)
	{
		try 
		{
			File sourceFolder = new File(source);
			File targetFolder = new File(target + "/" + sourceFolder.getName());
			if(!targetFolder.exists())
			{
				targetFolder.mkdirs();
				File[] files = sourceFolder.listFiles();
				for(File file : files)
				{
					if(file.isDirectory())
					{
						copyFolder(source + "/" + file.getName(), targetFolder.getPath());
					}
					else
					{
						copyFile(file, targetFolder.getPath() + "/" + file.getName());
					}
				}
			}
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * 复制文件
	 * @param file
	 * @param target
	 */
	public static void copyFile(File file, String target)
	{
		try 
		{
			byte[] temp = new byte[(int)file.length()];
			FileInputStream fis = new FileInputStream(file);
			FileOutputStream fos = new FileOutputStream(target);
			fis.read(temp);
			fos.write(temp);
			fos.flush();
            fos.close();
            fis.close();
		} 
		catch (Exception e)
		{
            e.printStackTrace();
		}
	}
	
	public static void main(String[] args)
	{
		StringBuffer prefix = new StringBuffer("var data = [");
		prefix.append("{tid: \"title_1\",id: \"chart_1\",xData: [\"衬衫\",\"羊毛衫\",\"雪纺衫\",\"裤子\",\"高跟鞋\",\"袜子\"],ico: [\"销量\"],datas: [{name: \"销量\",type: \"bar\",data: [100,150,180,210,120,170]}],title: \"销量统计表\"},");
		prefix.append("{tid: \"title_2\",id: \"chart_2\",xData: [\"一月\", \"二月\", \"三月\", \"四月\", \"五月\", \"六月\"],ico: [\"帽子\", \"衣服\", \"鞋子\"],datas: [{name: \"帽子\",type: \"bar\",data: [300,350,380,310,320,370]},{name: \"衣服\",type: \"bar\",data: [100,150,180,210,120,170]},{name: \"鞋子\",type: \"line\",data: [200,250,280,110,220,270]}],title: \"销量统计表2\"},");
		prefix.append("{tid: \"title_3\",id: \"chart_3\",xData: ['直接访问','邮件营销','联盟广告','视频广告','搜索引擎'],ico: [\"帽子\", \"衣服\", \"鞋子\"],piethemeColor:['#ffeeaa','#00ffcc','#eeddcc','#aabbcc','#ccddaa'],datas: [{name: \"访问来源\",type: \"pie\",data: [{value:335, name:'直接访问'},{value:310, name:'邮件营销'},{value:234, name:'联盟广告'},{value:135, name:'视频广告'},{value:1548, name:'搜索引擎'}]}],title: \"销量统计表3\"},");
		prefix.append("{tid: \"title_4\",id: \"chart_4\",xData: [\"一月\", \"二月\", \"三月\", \"四月\", \"五月\", \"六月\"],ico: [\"帽子\", \"衣服\", \"鞋子\"],datas: [{name: \"帽子\",type: \"bar\",data: [300,350,380,310,320,370]},{name: \"衣服\",type: \"bar\",data: [100,150,180,210,120,170]},{name: \"鞋子\",type: \"line\",data: [200,250,280,110,220,270]}],title: \"销量统计表4\"},");
		prefix.append("{tid: \"title_5\",id: \"chart_5\",xData: [\"一月\", \"二月\", \"三月\", \"四月\", \"五月\", \"六月\"],ico: [\"帽子\", \"衣服\", \"鞋子\"],datas: [{name: \"帽子\",type: \"bar\",data: [300,350,380,310,320,370]},{name: \"衣服\",type: \"bar\",data: [100,150,180,210,120,170]}],title: \"销量统计表5\"}]");
		
		String sf = "E:/project/frame/bi-front/views/released/released.js";
		String targetName = System.currentTimeMillis() + "";
		String tf = "E:/project/frame/bi-front/views/released/templateshow1/js/js_" + targetName + ".js";
		Map<String, String> replaceTxt = new HashMap<String, String>();
		copyFile(sf, tf, replaceTxt, prefix.toString());
		
		sf = "E:/project/frame/bi-front/views/released/templateshow1/index.html";
		tf = "E:/project/frame/bi-front/views/released/templateshow1/index_" + targetName + ".html";
		replaceTxt.put("\\s*on(dbl)?click[^\\)]+\\);?\"", "");
		replaceTxt.put("\"js/index.js\"", "\"js/js_" + targetName + ".js\"");
		
		copyFile(sf, tf, replaceTxt, "");

	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值