java后台创建压缩包&使用浏览器下载文件(批量下载实现)


项目中所提出的需求,需要将指定的页面生成为pdf文件,然后进行下载,同时需要支持批量下载。 

首先想到的就是先生成pdf文件,然后将其进行压缩,生成zip压缩文件,然后使用浏览器的下载功能即可完成批量下载的需求。

//点击批量下载按钮所触发的js片段
function getTeamReport(obj){
            var temp_form = document.createElement("form");
            temp_form.action = "xxx/xxx";//具体的action路径
            temp_form.target = "_blank";
            temp_form.method = "post";
            temp_form.style.display = "none";

            //传给后台的参数,用来查找需要下载的pdf文件的路径集合
            var params = {
                    param1: "xxx",
                    param2: "xxx",
                    ...
                    paramX: "xxx"
                };
            for ( var x in params) {
                var opt = document.createElement("textarea");
                opt.name = x;
                opt.value = params[x];
                temp_form.appendChild(opt);
            }
            document.body.appendChild(temp_form);
            temp_form.submit();
        }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24



因为项目中所需的pdf文件在第一次打开其对应页面时已经生成完毕,所以这里只需查出具体对应的文件路径即可,其他项目中如果pdf文件(其他类文件)还未生成,需要先生成

//后台接收的action
//批量导出报告
        @RequestMapping(value = "xxx/xxx",method = RequestMethod.POST )
        @ResponseBody
        public Map<String,Object> feedBackDirectMultiDownload(HttpServletRequest request,HttpServletResponse response) throws IOException{
            //压缩文件初始设置
            String path="压缩文件想要放置的路径";
            base_name = "zip文件名";
            fileZip = base_name + ".zip"; // 拼接zip文件
            filePath = path+"\\" + fileZip;//之后用来生成zip文件

            //filePathArr为根据前台传过来的信息,通过数据库查询所得出的pdf文件路径集合(具体到后缀),此处省略
            files = new File[fileNameArr.size()];//
            for(int i=0;i<fileNameArr.size();i++){
                files[i]=new File(fileNameArr.get(i).get("filePath"));//获取所有需要下载的pdf
            }

            // 创建临时压缩文件
            try {
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
                ZipOutputStream zos = new ZipOutputStream(bos);
                ZipEntry ze = null;
                for (int i = 0; i < files.length; i++) {//将所有需要下载的pdf文件都写入临时zip文件
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(files[i]));
                    ze = new ZipEntry(files[i].getName());
                    zos.putNextEntry(ze);
                    int s = -1;
                    while ((s = bis.read()) != -1) {
                        zos.write(s);
                    }
                    bis.close();
                }
                zos.flush();
                zos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //以上,临时压缩文件创建完成

            //进行浏览器下载       
            //获得浏览器代理信息
            final String userAgent = request.getHeader("USER-AGENT");
            //判断浏览器代理并分别设置响应给浏览器的编码格式
            String finalFileName = null;
            if(StringUtils.contains(userAgent, "MSIE")||StringUtils.contains(userAgent,"Trident")){//IE浏览器
                finalFileName = URLEncoder.encode(fileZip,"UTF8");
                System.out.println("IE浏览器");
            }else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器
                finalFileName = new String(fileZip.getBytes(), "ISO8859-1");
            }else{
                finalFileName = URLEncoder.encode(fileZip,"UTF8");//其他浏览器
            }
            response.setContentType("application/x-download");//告知浏览器下载文件,而不是直接打开,浏览器默认为打开
            response.setHeader("Content-Disposition" ,"attachment;filename=\"" +finalFileName+ "\"");//下载文件的名称

            ServletOutputStream servletOutputStream=response.getOutputStream();
            DataOutputStream temps = new DataOutputStream(
                            servletOutputStream);

            DataInputStream in = new DataInputStream(
                                    new FileInputStream(filePath));//浏览器下载文件的路径
            byte[] b = new byte[2048];
            File reportZip=new File(filePath);//之后用来删除临时压缩文件
            try {
                while ((in.read(b)) != -1) {
                temps.write(b);
            }
                temps.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(temps!=null) temps.close();
                if(in!=null) in.close();
                if(reportZip!=null) reportZip.delete();//删除服务器本地产生的临时压缩文件
                servletOutputStream.close();
            }
            return null;        
    }
原文出处:http://blog.csdn.net/yeyinglingfeng/article/details/72846368
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值