webwork 實現數據生成text文件,并進行壓縮,并進行下載

          //實現壓縮文件功能,采用commons-io-2.0.1.jar ,commons-compress-1.5.jar插件
                  final OutputStream out = new FileOutputStream("D:/EDI/EDi.zip");  //實例文件輸出流
                  ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, out);  
                  //實例化存檔輸出流,工廠方法創建zip的存檔輸出流
          //        File f1 = new File(file.getPath());
                  os.putArchiveEntry(new ZipArchiveEntry(file.getName()));  //生成存檔文件名
                  IOUtils.copy(new FileInputStream(file), os);  //添加拷貝存檔文件
                  
                  os.closeArchiveEntry();  
                  os.close();  
                  
                  //*************************
                  try {
                      File input = new File("D:/EDI/EDi.zip");//獲得下載文件路徑
                      contentType="application/octet-stream";
                      docStream = new FileInputStream(input);//獲得輸入流名稱
                      contentDisposition =URLEncoder.encode(input.getName() ,"UTF-8");
                     } catch (FileNotFoundException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                     }  
                     return "download";
          WEBWORK的文件下載機制。使用起來還是比較簡單的。
          下面是用法說明:
          首先在一個ACTION中,如果判斷有權限進行文件下載。
          則:
          1、讀出該下載文件,并生成一個流。 文件名應當從請求的request中讀出,或從用戶的表中取出。
          public String downLoadFile(String fileName){
             try {
              File input = new File("e:/engilish literature.doc");
              docStream = new FileInputStream(input);
              contentDisposition = "test.txt";
             } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }  
             return "download";
          }
          2、將輸出導向到一個特殊的RESULT中去。叫做Steam Result。
                   <action name="register" class="com.job2easy.web.user.RegisterAction">
                       <result name="success" type="dispatcher">
                           <param name="location">/home/register-result.jsp</param>
                       </result>
                       <result name="input">
                           <param name="location">/home/register.jsp</param>
                       </result>
                       <result name="download" type="stream">
                           <param name="contentType">application/x-msdownload</param>
                           <param name="inputName">docStream</param>
                           <param name="bufferSize">1024</param>              
                           <param name="contentDisposition">attachment;filename="${contentDisposition}"</param>
                       </result>

                       <interceptor-ref name="params"/>
                   </action>
          3、這中間有幾個參數需要配置:
               contentType設成 application/x-msdownload 就可以。這樣瀏覽器會保證彈出一個下載文件的對話框。
              inputName 這個比較重要,這個名字是輸入流的名稱, 以后要steam result的實現類中為根據OGNL的表達式去查找的。
              contentDisposition 這個是下載之后,保存在用戶端的文件名稱。${contentDisposition} 看一下代碼。如果寫成上述的方式,就有機會在ACTION中設置文件名。
          4、另外一個參數:contentLength就是下載文件的大小,webwork的stream result似乎實現有問題,不能根據文件的大小動態進行設置,只能寫死。
               這個參數的意義是告訴瀏覽下載的文件有多大,以便瀏覽器正確的顯示進度條。如果這個功能很重要的話,可以重新寫一個RESULT來實現。
          0

          posted on 2016-08-09 17:49 youngturk 閱讀(276) 評論(0)  編輯  收藏 所屬分類: 筆試題 、Java基礎 、struts2

          <2016年8月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導航

          統計

          公告

          this year :
          1 jQuery
          2 freemarker
          3 框架結構
          4 口語英語

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          EJB學習

          Flex學習

          learn English

          oracle

          spring MVC web service

          SQL

          Struts

          生活保健

          解析文件

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 稻城县| 德惠市| 西乌珠穆沁旗| 沙洋县| 高雄县| 乡城县| 汝阳县| 怀柔区| 库尔勒市| 五家渠市| 东阿县| 剑阁县| 股票| 邳州市| 平乐县| 思茅市| 车险| 吕梁市| 滨海县| 阳信县| 枞阳县| 广饶县| 鹤壁市| 锡林浩特市| 凤阳县| 长海县| 宜兴市| 梁河县| 齐河县| 舒兰市| 谢通门县| 和平县| 黑龙江省| 望谟县| 观塘区| 茌平县| 嘉禾县| 潼关县| 柏乡县| 临江市| 宝应县|