/**
* 模板下載
* @param event
* @roseuid 46A6B79D0399
*/
public void download() {
TemplateInfo entity = (TemplateInfo)JSFUtils.getManagedBeanValue("row");
FacesContext context =FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
String fileName = entity.getTemplatePath();
String filefullName = entity.getTemplateName();
String fileAllPath = request.getSession().getServletContext().getRealPath("http://")+"WEB-INF//template//"+filefullName;
System.out.println("path:"+fileAllPath);
ByteArrayOutputStream bos;
try {
bos = this.dowoLoadFile(fileAllPath);
//FacesContext context =FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse)context.getExternalContext().getResponse();
response.setHeader("Content-disposition","attachment; filename="+URLEncoder.encode(filefullName,"UTF-8"));
response.setContentLength(bos.size());
ServletOutputStream sos = response.getOutputStream();
bos.writeTo(sos);
bos.close();
sos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private ByteArrayOutputStream dowoLoadFile(String fileName) throws IOException {
FileInputStream f = new FileInputStream(fileName);
BufferedInputStream bu = new BufferedInputStream(f);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
BufferedOutputStream bo = new BufferedOutputStream(bao);
int i;
while((i=bu.read())!=-1) {
bo.write(i);
}
bo.flush();
bu.close();
return bao;
}
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。