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. opendir函数

    #include<sys/types.h> #include<dirent.h> DIR *dirptr = NULL; struct dirent *entry; dirpt ...

  2. MapReduce job.setNumReduceTasks(0)思考

    一.概述 在 http://zy19982004.iteye.com/blog/2037549的最后曾经提到过,这里再详细探讨一下. 二.job.setNumReduceTasks(0)唯一影响的是m ...

  3. 16个常用IO流

    在包java.io.*:下 有以下16个常用的io流类: (Stream结尾的是字节流,是万能流,通常的视频,声音,图片等2进制文件, Reader/Writer结尾的是字符流,字符流适合读取纯文本文 ...

  4. 对冲的艺术——delta中性交易

    delta中性交易 delta中性交易——外行话 delta中性交易就是构造一个含有期权头寸的组合,使其不受标的股票或指数价格小幅变动的影响.换句话讲,无论标的价格是涨还是跌,组合的市值始终保持不变. ...

  5. Qt 多线程和网络编程学习

    一,Qt多线程类学习 QThread类,开始一个新的线程就是开始执行重新实现QThread::run(),run()是默认现实调用exec(),QThread::start()开始线程的执行,run( ...

  6. windosw应用提示内存不足

    找到如下注册表分支: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\ CurrentVersion\Windows 在右侧窗口中可以看到名为&quo ...

  7. Linux系统安装之U盘引导

    折腾了一天,终于把系统安装上去了,心里那个苦啊. 参考:http://blog.csdn.net/avilifans/article/details/13768139 安装步骤如下: 第一步:Ultr ...

  8. 用word制作电子书最简捷模式 支持epub和mobi目录

    因为制作一本OCR的电子书,转到word编辑排版后,用calibre转成mobi发现没有目录,在网上查了资料研究了一下,终于解决了目录问题,根本不用将word文档转换为什么htm或txt,尤其是转换t ...

  9. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:5.安装Oracle RAC FAQ-RAC安装DB软件runInstaller看不到节点

    集群安装正常: [root@kmdbrac1 ~]# crs_stat -t -v Name Type R/RA F/FT Target State Host -------------------- ...

  10. [Andriod] - Andriod Studio + 逍遥模拟器

    Andriod Studio自身自带的模拟器实在太卡,用Genymotion模拟器又要安装VirtualBox,然后一堆的设置,结果还是卡B. 网上下了个逍遥模拟器,这模拟器是游戏专用的,目前正式版的 ...