Configuration freemarkerCfg = new Configuration();
//加载模版
ServletContext servletContext = htmlCarrier.getRequest().getSession().getServletContext();
freemarkerCfg.setServletContextForTemplateLoading(servletContext, Constants.HTML_TEMPLATE_PATH_BASE);
freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");
try {
//指定模版路径
Template template = freemarkerCfg.getTemplate(htmlCarrier.getTemplatePath(),"UTF-8");
template.setEncoding("UTF-8");
//静态页面路径
String htmlPath = null;
if(Constants.isOnServer){
htmlPath = Constants.SERVER_SAVEFILE_PATH+"/"+htmlCarrier.getTargetPath();
}
else {
htmlPath = servletContext.getRealPath("/")+htmlCarrier.getTargetPath();
}
File htmlFile = new File(htmlPath);
FileOutputStream fs = new FileOutputStream(htmlFile);
OutputStreamWriter os = new OutputStreamWriter(fs,"UTF-8");
Writer out = new BufferedWriter(os);
//处理模版
template.process(htmlCarrier.getData(), out);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
用浏览器模拟手机运行没有问题
手机用IP测试会报错java.lang.IllegalStateException: getOutputStream() has already been called for this response