<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 墻頭草 閱讀(3555) 評論(0)  編輯  收藏

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


    網站導航:
     
    人人游戲網 軟件開發網 貨運專家
    主站蜘蛛池模板: 亚洲欧洲中文日产| 伊人亚洲综合青草青草久热| 亚洲视频在线观看不卡| 人人揉揉香蕉大免费不卡| 亚洲精品色午夜无码专区日韩| 91av免费在线视频| 亚洲精品国产精品乱码不卡√| 中国一级特黄高清免费的大片中国一级黄色片 | 日韩人妻无码精品久久免费一| 亚洲第一福利视频| 91福利视频免费| 久久精品国产亚洲AV久| 毛色毛片免费观看| 国产区图片区小说区亚洲区| 亚洲国产精品成人久久蜜臀| CAOPORN国产精品免费视频| 亚洲国产精品一区二区成人片国内 | 黄色片免费在线观看| 亚洲AV无码精品色午夜在线观看| 免费成人高清在线视频| 久久久久亚洲AV无码麻豆| 青娱乐免费在线视频| 亚洲AV综合永久无码精品天堂| 国产免费卡一卡三卡乱码| 一区二区三区精品高清视频免费在线播放| 国产成人99久久亚洲综合精品| 爽爽爽爽爽爽爽成人免费观看| 亚洲视频免费在线看| 免费无码又爽又刺激高潮| 亚洲免费日韩无码系列| 亚洲宅男永久在线| 永久免费看bbb| 中文字幕在线免费看| 亚洲成在人线电影天堂色| 国产精品免费一级在线观看| 两个人看的www视频免费完整版| 亚洲福利一区二区精品秒拍| 白白国产永久免费视频| 在线成人精品国产区免费| 国产精品亚洲综合久久 | 亚欧免费视频一区二区三区 |