<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.根據(jù)畫面上的復(fù)選框進(jìn)行文件打包下載
    2.待下載文件保存在服務(wù)器的硬盤上,打包過程中不生成臨時(shí)文件
    3.打包過程中需要?jiǎng)討B(tài)創(chuàng)建一個(gè)txt文件一并打進(jìn)zip包中
    4.頁面上沒有文件被選擇的場合,按下【下載】按鈕后,什么都不做(不刷新頁面)

    部分內(nèi)容參考自互聯(lián)網(wǎng),如果錯(cuò)誤,歡迎指正。
    Struts配置文件
    <!-- 數(shù)據(jù)下載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 (有數(shù)據(jù)可下載) {;
                // 預(yù)處理
                preProcess();

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

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

        }

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

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

            //設(shè)定輸出文件頭
            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.動(dòng)態(tài)壓縮一個(gè)File到zip中
            // 創(chuàng)建一個(gè)ZipEntry,并設(shè)置Name和其它的一些屬性
            // 壓縮包中的路徑和文件名稱
            ze = new ZipEntry("1\\1\\" + file.getName());
            ze.setSize(file.length());
            ze.setTime(file.lastModified());

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

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

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

            // 利用ByteArrayInputStream把流數(shù)據(jù)寫入到客戶端
            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)  編輯  收藏

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


    網(wǎng)站導(dǎo)航:
     
    人人游戲網(wǎng) 軟件開發(fā)網(wǎng) 貨運(yùn)專家
    主站蜘蛛池模板: 国产成人免费手机在线观看视频 | 免费国产午夜高清在线视频 | 97人妻精品全国免费视频| 国产青草视频免费观看97| 亚洲一线产品二线产品| 在线观看人成视频免费| 亚洲老熟女五十路老熟女bbw| 久久电影网午夜鲁丝片免费| 亚洲一本之道高清乱码| 亚洲免费综合色在线视频| 亚洲无人区码一二三码区别图片 | 亚洲精品网站在线观看不卡无广告 | 成年大片免费视频| 亚洲中文字幕久久精品无码A| 精品免费国产一区二区| 亚洲精品乱码久久久久久蜜桃图片| 日韩免费观看的一级毛片| 国产成人亚洲综合在线| 亚洲无码高清在线观看| 久久久久免费精品国产| 亚洲国产视频网站| 免费毛片在线视频| 国产精品美女久久久免费 | 国产偷窥女洗浴在线观看亚洲 | 亚洲卡一卡二卡乱码新区| 国产成人免费福利网站| 国产va免费观看| 亚洲视屏在线观看| 无码一区二区三区免费视频| 一级做a爰片性色毛片免费网站 | 亚洲日本乱码在线观看| 99久久精品免费视频| 亚洲а∨天堂久久精品9966| 国产大片免费观看中文字幕| a毛片在线免费观看| 在线综合亚洲中文精品| 亚洲国产精品嫩草影院久久| 久久A级毛片免费观看| 国产AV无码专区亚洲AV麻豆丫 | 亚洲永久精品ww47| 99久久免费国产精品特黄|