/**
* 模板下载 * @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("//")+"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; } |
|