1、action

String templete=ConstantsAppParams.CONTRACT_TEMPLET_DOC;//contract_templet.tld
String templeteHtml=ConstantsAppParams.CONTRACT_TEMPLET_HTML;//contract_templetHTML.tld

P2pAppInfo pai = (P2pAppInfo) baseLogic.getEntityByPK(P2pAppInfo.class,new Long(request.getParameter("priNumber")));

Map map = FreeMarkerUtils.convertBean(pai);//将实体类转成MAP

String flag = request.getParameter("flag");
  if ("1".equals(flag)) {//预览
   FreeMarkerUtils.createWordFile(request,map,templeteHtml,date + pai.getLoanName() + "借款合同.html",pai.getLoanContractNo());
   StringBuffer path = new StringBuffer();
   String str = request.getContextPath();
   path.append(str);
   path.append(File.separator);
   path.append(ConstantsAppParams.CONTRACT__HTML);
   path.append(File.separator);
   path.append(pai.getLoanContractNo());
   path.append(File.separator);
   path.append(date + pai.getLoanName() + "借款合同.html");
   response.setCharacterEncoding("utf-8");
   PrintWriter writer = response.getWriter();
   writer.write(path.toString());
   writer.flush();
   writer.close();
  } else {//下载
   response.setContentType("application/msword");
   response.setHeader("Content-Disposition", "attachment;filename="
     + new String("借款合同".getBytes("gb2312"), "ISO8859-1")
     + ".doc");
//   response.set
   request.setCharacterEncoding("utf-8");
   response.setCharacterEncoding("utf-8");
   FreeMarkerUtils.downLoadWord(request, response,
     map,
     templete,
     date + pai.getLoanName() + "借款合同");
  }

2、FreeMarkerUtils

public class FreeMarkerUtils {
 /**
  * 生成预览的合同
  * @param request
  * @param response
  * @param dataMap
  * @param templateName
  * @param fileName
  * @throws Exception
  */
 public static void downLoadWord(HttpServletRequest request,HttpServletResponse response,Map dataMap,String templateName,String fileName) throws Exception{
         FreeMarkerConfigurationFactory fcf=new FreeMarkerConfigurationFactory();
         Configuration configuration = fcf.createConfiguration();
         fcf.setDefaultEncoding("utf-8");
         configuration.setDefaultEncoding("utf-8");
         String path=request.getSession().getServletContext().getRealPath("");
         path=path+File.separator+ConstantsAppParams.CONTRACT_TEMPLET_FOLDER;
          configuration.setDirectoryForTemplateLoading(new File(path));
            Template template = configuration.getTemplate(templateName);
            template.setEncoding("utf-8");
            //PrintWriter out = response.getWriter();
        
            OutputStreamWriter writer = new OutputStreamWriter(   response.getOutputStream(),"utf-8");//下载
            template.process(dataMap,writer);
            writer.flush();
            writer.close();
    }
 /**
  * bean 转map
  * @param bean
  * @return
  * @throws IntrospectionException
  * @throws IllegalAccessException
  * @throws InvocationTargetException
  */
  public static Map convertBean(Object bean)
             throws IntrospectionException, IllegalAccessException, InvocationTargetException {
         Class type = bean.getClass();
         Map returnMap = new HashMap();
         BeanInfo beanInfo = Introspector.getBeanInfo(type);
         PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
         for (int i = 0; i< propertyDescriptors.length; i++) {
             PropertyDescriptor descriptor = propertyDescriptors[i];
             String propertyName = descriptor.getName();
             if (!propertyName.equals("class")) {
                 Method readMethod = descriptor.getReadMethod();
                 Object result = readMethod.invoke(bean, new Object[0]);
                 if (result != null) {
                     returnMap.put(propertyName, result);
                 } else {
                     returnMap.put(propertyName, "");
                 }
             }
         }
         return returnMap;
     }
  /**
   * 生成可下载的合同
   * @param request
   * @param dataMap
   * @param templateName
   * @param string
  * @param string2
   * @throws Exception
   */
 public static void createWordFile(HttpServletRequest request,Map dataMap,
   String templateName, String string, String loanContractNo) throws Exception{
  FreeMarkerConfigurationFactory fcf=new FreeMarkerConfigurationFactory();
     Configuration configuration = fcf.createConfiguration();
     fcf.setDefaultEncoding("utf-8");
     configuration.setDefaultEncoding("utf-8");
     String path=request.getSession().getServletContext().getRealPath("");
     String filePath=path+File.separator+ConstantsAppParams.CONTRACT__HTML;
     path=path+File.separator+ConstantsAppParams.CONTRACT_TEMPLET_FOLDER;
      configuration.setDirectoryForTemplateLoading(new File(path));
        Template template = configuration.getTemplate(templateName);
        template.setEncoding("utf-8");
        filePath=filePath+File.separator+loanContractNo;
        File file=new File(filePath);
        if(!file.exists()){
         file.mkdir();
        }
        File file2=new File(filePath, string);
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file2),"UTF-8"));
        template.process(dataMap,out);
        out.flush();
        out.close();
 }
}

根据word模板(contract_templet.tld)生成并下载word合同及根据wordHTML模板(contract_templetHTML.tld)预览合同内容的更多相关文章

  1. 使用java的 htpUrlConnection post请求 下载pdf文件,然后输出到页面进行预览和下载

    使用java的 htpUrlConnection post请求 下载pdf文件,然后输出到页面进行预览和下载 2018年06月07日 10:42:26 守望dfdfdf 阅读数:235 标签: jav ...

  2. T4模板根据DB生成实体类

    1.前言 为什么会有这篇文章了,最近看到了一些框架,里面要写的代码太多了,故此就想偷懒,要是能写出一个T4模板,在数据库添加表后,根据模板就可以自动生成了类文件了,这样多好,心动不如行动.记得使用T4 ...

  3. POI生成Web版Word文件

    POI生成Web版Word文件 1       通过URL的输入流实现 2       直接把Html文本写入到Word文件 所谓的使用POI生成Web版Word文件是指利用POI将Html代码插入到 ...

  4. IOS 预览word文档的集中方式

    在iPhone中可以很方便的预览文档文件,如:pdf.word等等,这篇文章将以PDF为例.介绍三种预览PDF的方式,又分别从本地pdf文档和网络上的pdf文档进行对比. 预览本地PDF文档: 1.使 ...

  5. IOS 预览pdf,word文档的集中方式

    在iPhone中可以很方便的预览文档文件,如:pdf.word等等,这篇文章将以PDF为例.介绍三种预览PDF的方式,又分别从本地pdf文档和网络上的pdf文档进行对比. 预览本地PDF文档: 1.使 ...

  6. java实现在线预览--poi实现word、excel、ppt转html

    java实现在线预览 - -之poi实现word.excel.ppt转html 简介 java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服 ...

  7. word预览

    word+excle表格在线浏览 word.ppt.xls文件实现在线预览的方式比较简单可以直接通过调用微软的在线预览功能实现 (预览前提:资源必须是公共可访问的) 通过iframe直接引用微软提供的 ...

  8. XPath2Doc,一个半自动采集网页生成Word Docx文件的工具,带企查查和天眼查模板

    原始出处:https://www.cnblogs.com/Charltsing/p/XPath2Doc.html 很多人需要从网站采集一些数据填写Word模板,手工操作费时费力还容易出错,所以我给朋友 ...

  9. C#读取Word模板替换相应的字符串(标签)生成新的Word

    在平常工作中,生成word的方式主要是C#读取html的模板文件处理之后保存为.doc文件,这样的好处是方便,快捷,能满足大部分的需求.不过有些特殊的需求并不能满足,如要生成的Word为一个表格,只是 ...

随机推荐

  1. 解决Oracle在scott用户下创建视图(VIEW)权限不足的方法

    问题描述:在scott用户下创建视图的时候,报错:权限不足.(其他用户以此类推)解决方法: 以dba用户登录 sqlplus / as sysdba 赋予scott用户创建VIEW的权限 grant  ...

  2. jsonp解决跨域

    ajax请求: $.ajax({        type: "get",//必须使用get方式        async: false,        url: "htt ...

  3. cache manifest 格式

    下面说明书写 cache manifest 文件需要遵循的格式.1 首行必须是 CACHE MANIFEST.2 其后,每一行列出一个需要缓存的资源文件名.3 可根据需要列出在线访问的白名单.白名单中 ...

  4. 使用bat脚本添加JAVA_HOME和修改PATH

    add_jre.bat ::添加环境变量JAVA_HOME @echo off echo 添加java环境变量 set regpath=HKEY_LOCAL_MACHINE\SYSTEM\Curren ...

  5. mysql 索引查询的问题

    之前碰到过一个问题,本来数据量不大的一张表,查询结果反应特别慢,不知道是什么原因. 后来才得知,查询的反应速度与结果集大小有关.  结果集越小反应速度越快.

  6. lame边录音边转换

    文章出处:http://big-joe.blog.sohu.com/88902836.html void CViewTFreqList::RecFileInit(LPCTSTR sFileName,  ...

  7. 使用游戏引擎photon打造一款特殊的远程控制软件

    前言 本文主要是介绍photon引擎的一些基本用法,以及使用游戏引擎开发远控的优势 0x1 有一段时候对做游戏的unity开发有些兴趣,在找游戏服务端引擎的时候,突然发现了这款歪果人开发的游戏引擎ph ...

  8. mfc 调用Windows的API函数实现同步异步串口通信(源码)

    在工业控制中,工控机(一般都基于Windows平台)经常需要与智能仪表通过串口进行通信.串口通信方便易行,应用广泛. 一般情况下,工控机和各智能仪表通过RS485总线进行通信.RS485的通信方式是半 ...

  9. Windows下利用虚拟机运行FSL 安装和配置

    FSL是牛津大学FMRIB开发的用于分析功能磁共振影像的科研软件包. 运行要求:windows7或vista操作系统,10G硬盘空间,4G内存. 从FSL官网上下载Centos6的虚拟盘(FSLVm6 ...

  10. Python 获得对象内存占用内存大小 sys.getsizeof

    from sys import getsizeof class A(object): pass class B: pass for x in (None, 1, 1L, 1.2, 'c', [], ( ...