批量下载

 /**
     * 功能描述: 批量导出
     *
     * @param : typeContent (上传文件的路径)  userName(需批量下载的用户名)
     * @author Larissa
     * @date 2020/1/14 14:42
     */
    @RequestMapping("outAnnexAll")
    @ResponseBody
    public void outAnnexAll(HttpServletRequest request, HttpServletResponse response, String typeContent, String userName) {
        //响应头的设置
        response.reset();
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");

        //设置压缩包的名字
        String billname = "人事档案-" + userName;
        String downloadName = billname + ".zip";

        response.setHeader("Content-Disposition", "attachment; fileName=" + new String((downloadName).getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));

        ZipOutputStream zipos = null;
        DataOutputStream os = null;
        InputStream is = null;
        try {
            //设置压缩流:直接写入response,实现边压缩边下载

            zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
            zipos.setMethod(ZipOutputStream.DEFLATED); //设置压缩方法
            //循环将文件写入压缩流

            /*将字符串转换为集合*/
            String[] split = typeContent.split(",");

            String basePath = "";
            if (System.getProperty("os.name").toLowerCase().startsWith("win")) {//windows系统
                basePath = properties.getProperty("windows.path").trim();
            } else if (System.getProperty("os.name").toLowerCase().startsWith("lin")) { //linux
                basePath = properties.getProperty("linux.path").trim();
            } else if (System.getProperty("os.name").toLowerCase().startsWith("mac")) {
                // mac os
                basePath = System.getProperty("user.home") + properties.getProperty("mac.path").trim();
            }


            for (int i = 0; i < split.length; i++) {
                String filePath = basePath + File.separator + split[i];

                System.out.println("filePath===" + filePath);
                File file = new File(filePath);
                if (!file.exists()) {
                    //先设置编码
                    response.setContentType("text/html;charset=UTF-8");
                    try {
                        response.getWriter().write("<script type=\"text/javascript\">alert('文件不存在或已被移动');window.history.back();window.close(); </script>");
                    } catch (IOException e) {
                        log.error(e.getMessage(), e);
                    }
                } else {
                    //添加ZipEntry,并ZipEntry中写入文件流
                    zipos.putNextEntry(new ZipEntry(userName+"/"+file.getName()));//里面的一定为目录,否则虽下载成功,但不能查看
                    os = new DataOutputStream(zipos);
                    is = new FileInputStream(file);
                    //一定要这样,之前使用IoUtis.copy()方法,结果导致失败
                    byte[] b = new byte[1024*10];
                    int len =0;
                    while ((len = is.read(b))!=-1){
                        os.write(b,0,len);
                    }
                }

            }
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        } finally {
       //切记关闭流(不关闭会产生bug,被检测出来会扣工资)
            if (os != null) {
                try {
                    os.flush();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
            if (zipos != null) {
                try {
                    zipos.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值