zoukankan      html  css  js  c++  java
  • Web端文件打包.zip下载

    使用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         }
  • 相关阅读:
    算法学习之基础(背包 列队 栈) 习题1.3.9 补全括号
    LVS负载均衡DR模式部署
    SQL更改表架构
    BCP导入导出MsSql
    E. Holes(分块)
    hdu6230 Palindrome(manacher+树状数组)
    Suffix(hash+lcp+二分)
    k近邻法( k-nearnest neighbor)
    《机器学习》第三章——LDA
    《机器学习》第三章——对率回归
  • 原文地址:https://www.cnblogs.com/niutouzdq/p/4241620.html
Copyright © 2011-2022 走看看