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. ajax请求cookie有效性问题验证

    与人讨论时遇到一个问题,就是: 浏览器中后台发起的一个异步ajax请求,服务器做响应时,附带了cookie信息,那么后续对同域名下其他页面请求时,该cookie是否有效,会一并随请求提交到web服务器 ...

  2. 记录自己对EventLoop和性能问题处理的一点心得

    1.EventLoop 这里说的EventLoop不是指某一个具体的库或是框架,而是指一种程序实现结构.这种结构多是基于IO多路转接的API(select.poll.epoll之类)以reactor模 ...

  3. js中的break ,continue, return (转)

    面向对象编程语法中我们会碰到break ,continue, return这三个常用的关键字,那么关于这三个关键字的使用具体的操作是什么呢?我们在使用这三关键字的时候需要注意和需要理解的规则是什么呢? ...

  4. WebForm aspx页面传值---7种方式

    1.get方式 发送页 <form id="form1" runat="server">    <div>        <a h ...

  5. 自定义ANDROID中EDITTEXT中的HINT文本的大小

    EditText editText = (EditText) rootView.findViewById(R.id.et); // 新建一个可以添加属性的文本对象 SpannableString ss ...

  6. same story,different day

    多亏了这个重感冒,宏伟计划险些又断了. 我是说当然断了一天,但是比起鄙人过往的八千多个日子,喔,虚惊一场. 又看了段lietome,作为不会看眼色的人,这样的美剧太有吸引力了.想象一下,一个八分半美女 ...

  7. 自学 PHP,如何不走弯路?

    1.一本好书至关重要.如果这本书的知识非常深入,那么还是不要看了.对初学者来说只能是打击.因为很多东西都看不懂.一本知识较为浅显,并且说明非常详细,但是能让你上手的基础知识又非常完善的书籍就非常好.( ...

  8. dict

    1.创建新字典(根据语法和dict初始化方法) >>> my_dict={'a':1,'b':2,'c':3} >>> my_dict=dict({'a':1,'b ...

  9. 《java编程思想》读书笔记(一)开篇&第五章(1)

    2017 ---新篇章  今天终于找到阅读<java编程思想>这本书方法了,表示打开了一个新世界. 第一章:对象导论 内容不多但也有20页,主要是对整本书的一个概括.因为已经有过完整JAV ...

  10. SHOI 2009 会场预约 平衡树 STL练习

    题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...