java 数据库 html_JAVA 将数据库中的内容转化为HTML

/** * 将数据库中的内容转化为HTML *data 存在数据库内字段的内容 *formId是数据库内该条信息的ID *filePath是生成的html文件所存放的位置 * @param image * @return */ public void formToHtml(String data, String FormId, String filePath) { StringBuffer buf = new StringBuffer(""); buf.append(""); buf .append(""); buf.append("

sdsds"); buf.append(""); buf.append(""); buf.append(""); buf.append(data); buf.append(""); buf.append(""); String fileName = filePath + FormId + ".html"; ObjectOutputStream objOutputStream; try { objOutputStream = new ObjectOutputStream(new FileOutputStream( new File(fileName))); objOutputStream.writeObject(buf.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 将数据库中的内容转化为HTML *vmBasePath vm模板所存放的位置 例如D:\\projectPath\\attach\\html *vmTempName vm模板的名称 *dataContext 存在数据库内字段的内容 *HtmlFilePath 生成的html所存放的路径 例如:"D:\\projectPath\\attach\\html\\test.html" * @param image * @return */ public String dataToHtml(String vmBasePath, String vmTempName, String dataContext, String HtmlFilePath) throws Exception { // 初始化并取得Velocity引擎 VelocityEngine ve = new VelocityEngine(); // 设置路径 Properties properties = new Properties(); String basePath = vmBasePath; // String basePath = "D:\\projectPath\\attach\\html"; properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, basePath); ve.init(properties); // 取得velocity的模版 // Template t = ve.getTemplate("104.vm"); Template t = ve.getTemplate(vmTempName); // 取得velocity的上下文context VelocityContext context = new VelocityContext(); // 把数据填入上下文 // context.put("name", "Liang"); context.put("name", dataContext); // 输出流 StringWriter writer = new StringWriter(); // 转换输出 t.merge(context, writer); // PrintWriter filewriter = new PrintWriter(new // FileOutputStream("D:\\projectPath\\attach\\html\\test.html"), true); FileOutputStream fos = new FileOutputStream(HtmlFilePath); PrintWriter filewriter = new PrintWriter(fos, true); filewriter.println(writer.toString()); filewriter.flush(); filewriter.close(); fos.flush(); fos.close(); System.out.println("输出已经完成!"); return "success"; }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值