action通过VM生成文件并导出

//引擎
VelocityEngine ve = new VelocityEngine();
Properties props = new Properties();        
props.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, this.httpRequest.getSession().getServletContext().getRealPath("/") + "templates");
		
ve.init(props);
		
Map<String, Object> param = new HashMap<String, Object>();
param.put("currentYear", reportDate.substring(0, 4));

VelocityContext context = new VelocityContext();
context.put("param", param);
		
Template template = ve.getTemplate("monthSafeReport.vm","UTF-8");		
try 
{
    this.httpResponse.setContentType("application/vnd.ms-word;charset=UTF-8");
    this.httpResponse.setHeader("Content-Disposition", "attachment;filename=\""
			+ URLEncoder.encode("测试.doc", "UTF-8")
			+ "\"");
    OutputStream out = this.httpResponse.getOutputStream(); 
    Writer write = new OutputStreamWriter(out,"UTF-8");
    
    //转换输出
    template.merge(context, write);
    
    template.process();
    
    out.flush();
    write.flush();
    out.close();
    write.close();
} 
catch (Exception e) 
{
    e.printStackTrace();
}

你可能感兴趣的:(action通过VM生成文件并导出)