<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 5,  comments - 7,  trackbacks - 0
    通過JSF下載文件,不管這個文件是物理存在的,還是由服務器內存中生成的。
    用戶從頁面點擊連接(link),下載相關的文件,該文件存在服務器端,或者由服務器端生成文件流,沒有物理文件;下載后頁面不跳轉。

    JSP代碼:


    <h:commandLink actionListener="#{productBean.downloadAction}" styleClass="highLightLink">
       <h:outputText value="download"/>
       <f:param name="productId" value="#{productBean.id}"/>
    </h:commandLink>



    Backing bean設計及代碼:
    注意這是個Action listener方法,沒有返回值,并且有javax.faces.event.ActionEvent參數


       public void downloadAction(ActionEvent event) {
        try {
          String fileName="D:\\temp\\images\\products\\" + this.id + ".xls";
          logger.debug("file name=" + fileName);
          ByteArrayOutputStream baos=this.serviceLocator.getFileService().downloadFile(fileName); //調用Service方法,獲得文件的ByteArrayOutputStream
          HttpServletResponse response=FacesUtils.getServletResponse();
          response.setHeader("Content-disposition", "attachment; filename=" + id+ ".xls" ); //不是內嵌顯示(inline),而是作為附件下載
          response.setContentLength(baos.size());
          ServletOutputStream sos=response.getOutputStream();
          baos.writeTo(sos);
          baos.close();
          sos.flush();
        } catch (IOException ex) {      
          logger.debug(ex);
        }  
      }



    Service代碼:
    這個實現是一個從已經存在的物理文件獲得輸出流的范例,至于由Server在內存中生成輸出流也是一樣處理,例如生成一個Excel文件,再讓用戶下載。


    public ByteArrayOutputStream downloadFile(String fileName) throws IOException {
        FileInputStream fis=new FileInputStream(fileName);
        BufferedInputStream bis=new BufferedInputStream(fis);
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        BufferedOutputStream bos=new BufferedOutputStream(baos);
        
        int i;
        while((i=bis.read())!=-1) {
          bos.write(i);
        }
        bos.flush();//提交文件流,很關鍵
        bis.close();
        return baos;
      }

    posted on 2008-11-27 11:25 Vincent-chen 閱讀(610) 評論(0)  編輯  收藏 所屬分類: JSF
    主站蜘蛛池模板: 亚洲人成图片小说网站| 亚洲高清日韩精品第一区| 亚洲精品第一综合99久久| 久久狠狠躁免费观看| 亚洲精品白浆高清久久久久久| 一级做a爰片久久毛片免费陪 | 亚洲精品无码成人片久久不卡 | 无码精品一区二区三区免费视频| 亚洲熟妇中文字幕五十中出| 亚洲免费无码在线| 亚洲桃色AV无码| 久久永久免费人妻精品下载| 亚洲综合综合在线| 毛片免费视频在线观看| 亚洲最大中文字幕无码网站| 又大又硬又粗又黄的视频免费看| 中文字幕亚洲一区| 国产激情免费视频在线观看| 久久精品国产亚洲AV麻豆网站 | 国产精品无码一二区免费| 怡红院亚洲红怡院在线观看| 亚洲一区无码精品色| 国产一区二区三区免费观看在线 | 日本高清在线免费| 久久久久国产亚洲AV麻豆| 免费国产污网站在线观看| 亚洲啪啪免费视频| 国产色爽免费视频| 久久久受www免费人成| 99久久亚洲精品无码毛片| 成人免费视频小说| 一级做a爰片久久毛片免费看| 亚洲AV综合色区无码一区| 99re热免费精品视频观看| 色婷婷精品免费视频| 一区二区三区亚洲| 日韩一级视频免费观看| 日本免费在线中文字幕| 午夜在线a亚洲v天堂网2019| 国产成人毛片亚洲精品| 国产电影午夜成年免费视频 |