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

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

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

    posts - 241,  comments - 116,  trackbacks - 0
    功能:zip文件下載
    簡述:
    1.根據畫面上的復選框進行文件打包下載
    2.待下載文件保存在服務器的硬盤上,打包過程中不生成臨時文件
    3.打包過程中需要動態創建一個txt文件一并打進zip包中
    4.頁面上沒有文件被選擇的場合,按下【下載】按鈕后,什么都不做(不刷新頁面)

    部分內容參考自互聯網,如果錯誤,歡迎指正。
    Struts配置文件
    <!-- 數據下載Action -->
    <action name="downZip" class="DownloadAction">
        <result name="nodata" type="httpheader">
            <param name="status">sky7034</param>
        </result>
    </action>
    Action代碼
        private OutputStream res;
        private ZipOutputStream zos;

        // action的主方法
        public String execute() throws Exception {
            
            if (有數據可下載) {;
                // 預處理
                preProcess();

            } else {
                // 沒有文件可下載的場合,返回nodata,設定參照struts配置文件
                return "nodata";
            }

            // 在這里編輯好需要下載的數據風之境地
            // 文件可以是硬盤上的
            // 文件也可以是自己寫得數據流,如果是自己寫得數據流,請參看outputZipFile方法中的【2.】
            File file = new File();
            file = ...
            outputZipFile(file);
            
            // 后處理
            afterProcess();
            
            return null;

        }

        // 預處理
        public void preProcess() throws Exception {
            
            HttpServletResponse response = ServletActionContext.getResponse();
            
            res = response.getOutputStream();

            //清空輸出流
            response.reset();   

            //設定輸出文件頭
            response.setHeader("Content-disposition ","attachment; filename=a.zip ");   
            response.setContentType("application/zip");
            zos = new ZipOutputStream(res);
            
        }

        // 后處理
        public void afterProcess() throws Exception {
            zos.close();
            res.close();
        }

        // 寫文件到客戶端
        private void outputZipFile(File file) throws IOException, FileNotFoundException {
            ZipEntry ze = null;
            byte[] buf = new byte[1024];
            
            int readLen = 0;
            
            // 1.動態壓縮一個File到zip中
            // 創建一個ZipEntry,并設置Name和其它的一些屬性
            // 壓縮包中的路徑和文件名稱
            ze = new ZipEntry("1\\1\\" + file.getName());
            ze.setSize(file.length());
            ze.setTime(file.lastModified());

            // 將ZipEntry加到zos中,再寫入實際的文件內容
            zos.putNextEntry(ze);
            InputStream is = new BufferedInputStream(new FileInputStream(file));

            // 把數據寫入到客戶端
            while ((readLen = is.read(buf, 0, 1024)) != -1) {
                zos.write(buf, 0, readLen);
            }
            is.close();
            
            // 2.動態壓縮一個String到zip中
            String customFile = "This is a text file.";

            // 壓縮包中的路徑和文件名稱
            ZipEntry cze = new ZipEntry(“1\\1\\” + "Test.txt");
            zos.putNextEntry(cze);

            // 利用ByteArrayInputStream把流數據寫入到客戶端
            is = new ByteArrayInputStream(customFile.getBytes());
            while ((readLen = is.read(buf, 0, 1024)) != -1) {
                zos.write(buf, 0, readLen);
            }
            
        }
    posted on 2012-01-10 09:12 墻頭草 閱讀(3547) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    人人游戲網 軟件開發網 貨運專家
    主站蜘蛛池模板: 成全在线观看免费观看大全| 51精品视频免费国产专区| 久久久久亚洲av无码专区蜜芽| 久久久久免费看黄a级试看| 亚洲一区二区三区国产精品无码| 午夜神器成在线人成在线人免费 | 亚洲男人天堂av| 成年女人免费碰碰视频| 国产人成网在线播放VA免费| 久久亚洲精品无码aⅴ大香| 日韩电影免费在线| 精品国产污污免费网站| 亚洲依依成人亚洲社区| 亚洲色精品vr一区二区三区| 日本XXX黄区免费看| 一进一出60分钟免费视频| 亚洲免费福利视频| 91麻豆精品国产自产在线观看亚洲 | 免费精品国产日韩热久久| 日韩在线视频免费| 亚洲性无码av在线| 国产亚洲精午夜久久久久久| 久久久www成人免费毛片| 97在线免费观看视频| 男人的天堂av亚洲一区2区| 无码乱人伦一区二区亚洲一| 免费国产成人午夜私人影视| 四虎在线成人免费网站| a级毛片无码免费真人久久| 国产精品亚洲精品爽爽| 亚洲国产高清视频在线观看| 在线观看亚洲av每日更新| 免费精品国产自产拍观看| 永久免费av无码不卡在线观看 | 亚洲国产精品丝袜在线观看| 成人性生交大片免费看无遮挡 | 亚洲愉拍99热成人精品热久久| 日本三级2019在线观看免费| rh男男车车的车车免费网站| 亚洲人JIZZ日本人| 久久成人国产精品免费软件|