隨筆-348  評論-598  文章-0  trackbacks-0

          項目中所有的文檔全部壓縮之后存儲到數據庫,所以需要我們每次從數據庫讀取二進制然后發送到客戶端進行下載。如何在JSF里面這樣做呢?
          首先對外的方法這樣寫,我設置了按鈕的actionListener的屬性

              public void viewContent(ActionEvent event)
              
          {
                  
          try
                  
          {
                      ParkLawDTO obj 
          = this.getEnterpriseEnterInBODelegate().getParkLawById(((ParkLawDTO)this.getParkLawDataModel().getRowData()).getParkLaw().getPlId());
                      Common.downloadFile(FacesContext.getCurrentInstance(), ZipHelper.unzip(obj.getParkLaw().getLawContent()), obj.getParkLaw().getLawName()
          +".doc""application/msword;charset=utf-8");
                  }
          catch(Exception e){
                      info(
          "下載文件出錯", e);
                  }

                  
              }
          第一句是從WebService獲得對象的實例,第二句是將所有相應信息傳輸給下載的公共方法,同時解壓縮了相應的二進制。

          下面是下載的公共方法
          /**
               * 向客戶端發送需要下載的文件
               * 
          @param faces 當前FacesContext
               * 
          @param content 文件的字節數組
               * 
          @param fileName 客戶端接收的文件名
               * 
          @param contentType http的content-type
               
          */

              
          public static void downloadFile(FacesContext faces, byte[] content, String fileName, String contentType)
              
          {
                  
          try
                  
          {
                      HttpServletResponse response 
          = (HttpServletResponse) faces.getExternalContext().getResponse();    
                      response.setHeader(
          "Content-disposition""filename="+ URLEncoder.encode(fileName, "utf-8"));
                      response.setContentType(contentType);
                      response.setContentLength(content.length);
                      ServletOutputStream sos 
          = response.getOutputStream();
                      sos.write(content);
                      sos.flush();
                      sos.close();
                      
          //需要呼叫Complete
                      faces.responseComplete();
                     
                  }
          catch(Exception e){
                      e.printStackTrace();
                  }

              }

          Content-disposition中一定不可以加attachment,經過測試,在IE6下(其他瀏覽器沒測試過),會導致無法打開臨時文件(我的是word文檔),而且經常出現無法下載文件。

          ---------------------------------------------------------
          專注移動開發

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2008-12-23 10:13 TiGERTiAN 閱讀(1402) 評論(0)  編輯  收藏 所屬分類: JavaJSF
          專注移動開發--Windows Mobile, Android, iPhone, J2ME, BlackBerry, Symbian, Windows Phone

          慢慢混,慢慢學
          <2008年12月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(43)

          隨筆分類(402)

          隨筆檔案(306)

          相冊

          我的好友們

          搜索

          •  

          積分與排名

          • 積分 - 814019
          • 排名 - 50

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 武陟县| 阳谷县| 县级市| 丰都县| 黄大仙区| 油尖旺区| 略阳县| 五常市| 曲松县| 乌拉特前旗| 定兴县| 远安县| 潞西市| 永新县| 泰宁县| 商水县| 汕头市| 独山县| 五华县| 昌乐县| 高密市| 财经| 和政县| 民乐县| 遂宁市| 五莲县| 淮南市| 陇南市| 武平县| 陵川县| 水城县| 界首市| 扎囊县| 和平县| 赤城县| 全南县| 盐边县| 碌曲县| 曲阳县| 时尚| 嘉兴市|