最近做项目时需要完成流程模板的下载功能。在下载的过程中需要向审计系统发送审计日志。
流程模板存放在oracle数据库的blob类型的字段中。如下图所示:
现在需要在jsp页面中给用户一个“下载”按钮,点击后弹出文件保存框。
可以把文件保存到指定的路径下。但是现在有个问题:我文件保存成功后,程序会再次执行action中的下载方法。这样我的审计日志就会发送两次。请问这个是什么原因以及怎么来解决这个问题?
代码如下:
templateCfg.jsp中
用一个隐藏的iframe:
<iframe id="wf_download" src="" style="display: none"></iframe>
WfTemplateCfgAction.java 代码如下:
try {
Template template =wfTemplateCfgService.findTemplateWithContentByPk(tpPk);
byte[] content= template.getContent();
String wfName = template.getName();
wfName = wfName.substring(0, wfName.lastIndexOf("_"));
HttpServletResponse response = Struts2Utils.getResponse();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(wfName,"UTF-8")+".xml");
response.addHeader("Content-Length", "" + content.length);
response.setContentType("text/plain");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(content);
toClient.flush();
toClient.close();
} catch (Exception e) {
log.error(sm.getString("errorWhenReadFile"));
e.printStackTrace();
}
return null;
}
}