public String xmlFormwork(String xmlPath, String xmlName, String format, Map<String, Object> dataMap) { String xmlString = ""; // 得FreeMarker配置对象 // 创建Configuration对象 Configuration cfg = new Configuration(new Version("2.3.28")); // 设置模板编码格式 cfg.setEncoding(Locale.getDefault(), format); // 得FreeMarker的关键对象---------模板 // 创建Template对象 Template template = null; try { // 设置FreeMarker的模版文件位置 cfg.setDirectoryForTemplateLoading(new File(xmlPath)); cfg.setDefaultEncoding("UTF-8"); template = cfg.getTemplate(xmlName); } catch (IOException e1) { e1.printStackTrace(); } // String path = ServletActionContext.getServletContext().getRealPath("/"); /* * File dir = new File(path + htmlPath); if (!dir.exists()) { dir.mkdirs(); } */ // File fileName = new java.io.File(path + htmlPath + htmlName); // System.out.println("html file:" + fileName.getPath()); // Writer writer = null; StringWriter w = new StringWriter(); try { System.out.println(w.toString()); // 生成xml template.process(dataMap, w); w.close(); // System.out.println("w.toString()"); System.out.println(w.toString()); System.out.println("toString()"); xmlString = w.toString(); } catch (Exception e) { e.printStackTrace(); } return xmlString; }