• java代码实现打包多个文件下载功能


    //传入对应的需要打包的file 集合对象

    //文件打包下载
        public static HttpServletResponse downLoadFiles(List<File> files,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
            try {
                /**这个集合就是你想要打包的所有文件,
                 * 这里假设已经准备好了所要打包的文件*/
                //List<File> files = new ArrayList<File>();
        
                /**创建一个临时压缩文件,
                 * 我们会把文件流全部注入到这个文件中
                 * 这里的文件你可以自定义是.rar还是.zip

         这里的file路径发布到生产环境时可以改为

      */
                File file = new File(request.getSession().getServletContext().getRealPath("/qrcode.ara"));
                if (!file.exists()){  
                    file.createNewFile();  
                }
                response.reset();
                //response.getWriter()
                //创建文件输出流
                FileOutputStream fous = new FileOutputStream(file);  
                /**打包的方法我们会用到ZipOutputStream这样一个输出流,
                 * 所以这里我们把输出流转换一下*/
    //            org.apache.tools.zip.ZipOutputStream zipOut
    //                = new org.apache.tools.zip.ZipOutputStream(fous);
               ZipOutputStream zipOut
                = new ZipOutputStream(fous);
                /**这个方法接受的就是一个所要打包文件的集合,
                 * 还有一个ZipOutputStream*/
               zipFile(files, zipOut);
                zipOut.close();
                fous.close();
               return downloadZip(file,response);
            }catch (Exception e) {
                    e.printStackTrace();
                }
                /**直到文件的打包已经成功了,
                 * 文件的打包过程被我封装在FileUtil.zipFile这个静态方法中,
                 * 稍后会呈现出来,接下来的就是往客户端写数据了*/
               // OutputStream out = response.getOutputStream();
              
        
            return response ;
        }

      /**
         * 把接受的全部文件打成压缩包
         * @param List<File>; 
         * @param org.apache.tools.zip.ZipOutputStream 
         */
        public static void zipFile
                (List files,ZipOutputStream outputStream) {
            int size = files.size();
            for(int i = 0; i < size; i++) {
                File file = (File) files.get(i);
                zipFile(file, outputStream);
            }
        }

        public static HttpServletResponse downloadZip(File file,HttpServletResponse response) {
            try {
            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();

            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
            } catch (IOException ex) {
            ex.printStackTrace();
            }finally{
                 try {
                        File f = new File(file.getPath());
                        f.delete();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            }
            return response;
        }

    /** 
         * 根据输入的文件与输出流对文件进行打包
         * @param File
         * @param org.apache.tools.zip.ZipOutputStream
         */
        public static void zipFile(File inputFile,
                ZipOutputStream ouputStream) {
            try {
                if(inputFile.exists()) {
                    /**如果是目录的话这里是不采取操作的,
                     * 至于目录的打包正在研究中*/
                    if (inputFile.isFile()) {
                        FileInputStream IN = new FileInputStream(inputFile);
                        BufferedInputStream bins = new BufferedInputStream(IN, 512);
                        //org.apache.tools.zip.ZipEntry
                        ZipEntry entry = new ZipEntry(inputFile.getName());
                        ouputStream.putNextEntry(entry);
                        // 向压缩文件中输出数据  
                        int nNumber;
                        byte[] buffer = new byte[512];
                        while ((nNumber = bins.read(buffer)) != -1) {
                            ouputStream.write(buffer, 0, nNumber);
                        }
                        // 关闭创建的流对象  
                        bins.close();
                        IN.close();
                    } else {
                        try {
                            File[] files = inputFile.listFiles();
                            for (int i = 0; i < files.length; i++) {
                                zipFile(files[i], ouputStream);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

     

    //原文地址 http://blog.csdn.net/hbhgjiangkun/article/details/7282629

  • 相关阅读:
    C++复制构造函数,类型转换构造函数,析构函数,引用,指针常量和常量指针
    POJ1611(The Suspects)--简单并查集
    最小生成树-Kruskal算法
    POJ1861(Network)-Kruskal
    POJ1979(Red and Black)--FloodFill
    [转]全网最!详!细!tarjan算法讲解
    POJ1573(Robot Motion)--简单模拟+简单dfs
    最小生成树-Prim算法
    POJ3368(Frequent values)--线段树
    POJ3255(Roadblocks)--次短路径
  • 原文地址:https://www.cnblogs.com/laotan/p/4427952.html
Copyright © 2020-2023  润新知