zoukankan      html  css  js  c++  java
  • 根据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();
     }
    }

  • 相关阅读:
    Spring Cloud微服务安全实战_5-4_认证服务器使用spring session
    Spring Cloud微服务安全实战_5-3_基于session的SSO
    Spring Cloud微服务安全实战_5-2_实现授权码认证流程&实现SSO初步
    Spring Cloud微服务安全实战_5-1_单点登录基本架构
    Linux启动Spring boot项目命令
    Spring Cloud微服务安全实战_4-10_用spring-cloud-zuul-ratelimit做限流
    Spring Cloud微服务安全实战_4-9_用zuul网关解耦安全逻辑和业务逻辑
    Spring Cloud微服务安全实战_4-8_网关安全_01
    Spring Cloud微服务安全实战_4-7_token&client信息持久化到数据库
    几个好用Maven 镜像仓库地址
  • 原文地址:https://www.cnblogs.com/Defry/p/5213589.html
Copyright © 2011-2022 走看看