分享

fdf

 WindySky 2007-10-16
    /**
     * 模板下载
     * @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;
     }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约