文件打下载及zip打包下载

public String downloadFile(HttpServletRequest req,HttpServletResponse response){
    String basePath=request.getSession().getServletContext().getRealPath("/upload/fileDir");
        System.out.println(basePath);
        String fileName = "1.jpg";
        String targetPath = basePath+File.separator+fileName;
        //模拟多一个文件,用于测试多文件批量下载
        String targetPath1 = basePath+File.separator+"2.jpg";
        //模拟文件路径下再添加个文件夹,验证穷举
        String targetPath2 = basePath+File.separator+"test";
        System.out.println("文件名:"+fileName);
        System.out.println("文件路径:"+targetPath);

        //方法1:IO流实现下载的功能
        res.setContentType("text/html; charset=UTF-8"); //设置编码字符
        res.setContentType("application/octet-stream"); //设置内容类型为下载类型
        res.setHeader("Content-disposition", "attachment;filename="+fileName);//设置下载的文件名称
        OutputStream out = res.getOutputStream();   //创建页面返回方式为输出流,会自动弹出下载框
    
/*    //方法1-1:IO字节流下载,用于小文件
        System.out.println("字节流下载");
        InputStream is = new FileInputStream(targetPath);  //创建文件输入流
        byte[] Buffer = new byte[2048];  //设置每次读取数据大小,即缓存大小
        int size = 0;  //用于计算缓存数据是否已经读取完毕,如果数据已经读取完了,则会返回-1
        while((size=is.read(Buffer)) != -1){  //循环读取数据,如果数据读取完毕则返回-1
            out.write(Buffer, 0, size); //将每次读取到的数据写入客户端
        }
        is.close();
        */

/*    //方法1-2:IO字符流下载,用于大文件
        System.out.println("字符流");
        File file = new File(targetPath);  //创建文件
        FileInputStream fis=new FileInputStream(file);  //创建文件字节输入流
        BufferedInputStream bis=new BufferedInputStream(fis); //创建文件缓冲输入流
        byte[] buffer = new byte[bis.available()];//从输入流中读取不受阻塞
        bis.read(buffer);//读取数据文件
        bis.close();
        out.write(buffer);//输出数据文件
        out.flush();//释放缓存
        out.close();//关闭输出流
*/
        String zipBasePath = request.getSession().getServletContext().getRealPath("upload/zip");
        String zipName = "temp.zip";
        String zipFilePath=zipBasePath+File.separator+zipName;
        //创建需要下载文件的集合
        List<String> listPaths = new ArrayList();
        listPaths.add(targetPath);
        listPaths.add(targetPath1);
        listPaths.add(targetPath2);
        //压缩文件
        File zip = new File(zipFilePath);
        if(!zip.exists()){
            zip.createNewFile();
        }
        //创建zip文件输出流
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("zip"));
        this.zipFile(zipBasePath,zipName,zipFilePath,listPaths,zos);
        zos.close();
        res.setHeader("Content-disposition","attachment;filename="+zipName);
        //将打包后的文件写到客户端,使用缓冲流输出
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(zipFilePath));
        //创建字节数组存储流数据
        byte [] buff = new byte[bis.available()];
        bis.read(buff);
        bis.close();
        out.write(buff);
        out.flush();
        out.close();
        //char [] ch = new char[1024];
        /*bis.read(buff);
        bis.close();
        out.write(buff);
        out.flush();
        out.close();*/

        return null;
    }

    /**
     * 压缩文件
     * @param zipBasePath 临时压缩文件基础路径
     * @param zipName 临时压缩文件名称
     * @param zipFilePath 临时压缩文件完整路径
     * @param filePaths 需要压缩的文件路径集合
     * @throws IOException
     */
    private String zipFile(String zipBasePath, String zipName, String zipFilePath, List<String> filePaths,ZipOutputStream zos) throws IOException {

        //循环读取文件路径集合,获取每一个文件的路径
        for(String filePath : filePaths){
            File inputFile = new File(filePath);  //根据文件路径创建文件
            if(inputFile.exists()) { //判断文件是否存在
                if (inputFile.isFile()) {  //判断是否属于文件,还是文件夹
                    //创建输入流读取文件
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputFile));

                    //将文件写入zip内,即将文件进行打包
                    zos.putNextEntry(new ZipEntry(inputFile.getName()));

                    //写入文件的方法,同上
                    int size = 0;
                    byte[] buffer = new byte[1024];  //设置读取数据缓存大小
                    while ((size = bis.read(buffer)) > 0) {
                        zos.write(buffer, 0, size);
                    }
                    //关闭输入输出流
                    zos.closeEntry();
                    bis.close();

                } else {  //如果是文件夹,则使用穷举的方法获取文件,写入zip
                    try {
                        File[] files = inputFile.listFiles();
                        List<String> filePathsTem = new ArrayList<String>();
                        for (File fileTem:files) {
                            filePathsTem.add(fileTem.toString());
                        }
                        return zipFile(zipBasePath, zipName, zipFilePath, filePathsTem,zos);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值