根据word模板(contract_templet.tld)生成并下载word合同及根据wordHTML模板(contract_templetHTML.tld)预览合同内容

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();
 }
}

上一篇:解决FF浏览器无法执行window.close()脚本


下一篇:Linux 5.16 稳定版将带来诸多功能更新