java多线程生成二维码打包zip下载

干货直接上代码

本文讲解

本文是通过前端传递的楼id,查询到楼内的所有床位然后生成所有床位二维码再打包成zip包下载

使用到的工具类下载地址 链接: https://pan.baidu.com/s/16NLAZMbQQT9sZK5eHHvA_g 提取码: wq4p
Maven依赖

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.0.0</version>
        </dependency>

后端代码 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    @GetMapping("/download/{dormId}")
    @ApiOperation("二维码下载")
    public void downloadCode(@PathVariable String dormId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
        //按宿舍楼id查询所有床位
        List<Bunk> bunkList = bunkService.getByDormIdBunkAll(dormId);

        String realPath = request.getSession().getServletContext().getRealPath("/");
        //1 生成二维码
        String path = realPath + dormitory.getTowerName();
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        ///每条线程的数据数量
        int threadSize = 200;
        // 总数据条数
        int dataSize2=bunkList.size();
        // 线程数
        int threadNum = dataSize2 / threadSize + 1;
        // 定义标记,过滤threadNum为整数
        boolean special = dataSize2 % threadSize == 0;

        // 创建一个线程池
        ExecutorService exec = Executors.newFixedThreadPool(threadNum);
        // 定义一个任务集合
        List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
        Callable<Integer> task = null;
        List<Bunk> bunkCall = null;
        ///
        // 确定每条线程的数据
        for (int i = 0; i < threadNum; i++) {
            if (i == threadNum - 1) {
                if (special) {
                    break;
                }
                bunkCall = bunkList.subList(threadSize * i, dataSize2);
            } else {
                bunkCall = bunkList.subList(threadSize * i, threadSize * (i + 1));
            }
            final List<Bunk> bunkListStr = bunkCall;
            task = new Callable<Integer>() {
                Integer count = 0;

                @Override
                public Integer call() throws Exception {
                        for (Bunk bunk : bunkListStr) {
                            QRCodeUtil.generateQRImage(
                                    InspectionConstant.inspectionType.DORM_BED.toString() + "|" + bunk.getId(),
                                    path + "/" + bunk.getRoomId() + "号宿舍" + bunk.getNumber() + "床位" + ".jpg",
                                    null,bunk.getNumber()
                            );//生成二维码的方法
                        }
                        count++;
                    return 1;
                }
            };
            // 这里提交的任务容器列表和返回的Future列表存在顺序对应的关系
            tasks.add(task);
        }
            //开始执行线程任务
            List<Future<Integer>> results = exec.invokeAll(tasks);

        // 关闭线程池
        exec.shutdown();
        //2 生成zip文件
        ZipHelper.zipCompress(path, path + ".zip");
        //3 下载
        String zipFileName = path + ".zip";
        String filename =  dormitory.getTowerName()+ ".zip";
        //设置文件MIME类型
        response.setContentType("application/octet-stream");

        response.setCharacterEncoding("UTF-8");
        //设置Content-Disposition
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "utf-8"));
        InputStream in = new FileInputStream(zipFileName);
        OutputStream out = response.getOutputStream();

        //写文件
        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        out.flush();
        //4 删除多余文件
        ZipHelper.deleteDir(new File(path));
        in.close();//先关闭输入流才能删除
        ZipHelper.deleteDir(new File(zipFileName));
        out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

前端代码 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
前端页面直接访问到后台的controller即可下载

window.location.href = '/school/dormitory/download/' + row.id;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值