Web端檔案打包.zip下載

來源:互聯網
上載者:User

標籤:

使用ant.jar包的API進行檔案夾打包。直接上代碼:

 1             String zipfilename = "test.zip"; 2             File zipfile = new File(zipfilename); 3             org.apache.tools.ant.types.FileSet fileSet = new FileSet(); 4             org.apache.tools.ant.Project prj = new Project(); 5             org.apache.tools.ant.taskdefs.Zip zip = new Zip(); 6             zip.setProject(prj); 7             zip.setDestFile(zipfile); 8             fileSet.setProject(prj); 9             fileSet.setDir(file); // 這裡是檔案路徑的設定。10             zip.addFileset(fileSet);11             zip.execute();12             HttpServletResponse response = super.currentResponse;13             response.setHeader("Content-Type", "application/zip");14             response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(zipfilename, "UTF-8"));15             OutputStream os = response.getOutputStream();16             InputStream is = new FileInputStream(zipfile);17             DownloadUtil.transfer(is, os);18             zipfile.delete();

 如果在下載的時候,不希望下載檔案路徑下的所有檔案,而是部分符合自己需求的檔案的話。

下面這個例子,除了check了需求之外,還遠端的檔案進行了操作,利用ChannelSftp取得的串連,進行檔案讀入並下載。

如下:

private com.jcraft.jsch.ChannelSftp         channel; // 這行是類成員變數
 1 String zipfilename = getServerZipFileName(inputfilename); 2  3         if (channel == null || !channel.isConnected()) { 4             channel = SftpUtils.connect(sftpHost, Integer.parseInt(sftpPort), sftpUsername, sftpPassword);// 取得串連。 5         } 6  7         HttpServletResponse response = super.currentResponse; 8  9         try {10             boolean isMultiFilesName = isMultiFilesName(inputfilename);11             Vector <com.jcraft.jsch.ChannelSftp.LsEntry> vs = channel.ls(logfilepath);12             if (vs.isEmpty()) {13                 return false;14             } else {15                 response.setHeader("Content-Type", "application/octet-stream");16                 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(zipfilename, "UTF-8"));17                 OutputStream out = response.getOutputStream();18                 java.util.zip.ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(out));19                 for (LsEntry entry : vs) {20                     if (entry.getAttrs().toString().startsWith("-")) {21                         String entryname = entry.getFilename();22                         if (isMultiFilesName && entryname.startsWith(inputfilename.substring(0, inputfilename.length() - 1))23                                 && !entryname.matches("^.*[\\*]+.*$") || entryname.equals(inputfilename)) { // 符合規則的檔案才做輸出處理24                             java.util.zip.ZipEntry zipentry = new ZipEntry(entryname);25                             zipOut.putNextEntry(zipentry);26                             InputStream bis = channel.get(logfilepath + entryname); // 這裡要注意,目標目錄下的檔案名稱,不能有*,不然這一步get方法的目標可能是多個檔案而出錯。27                             if (bis != null) {28                                 int count = 0;29                                 byte[] buffer = new byte[8192];30                                 while ((count = bis.read(buffer)) > 0) {31                                     zipOut.write(buffer, 0, count);32                                 }33                                 bis.close();34                             }35                         }36                     }37                 }38                 zipOut.close();39             }40         } catch (Exception e) {41             response.reset();42             e.printStackTrace();43             return false;44         } finally {45             if (channel != null && !channel.isClosed()) {46                 channel.getSession().disconnect();47                 channel.disconnect();48             }49         }

 

Web端檔案打包.zip下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.