JAVA编码(66)—— 根据机构名称批量生成而扫描二维码图片并进行压缩下载(项目实例)...

本文介绍了一个用于批量生成特定机构二维码的方法。该方法通过查询数据库获取机构相关信息,并为每个机构生成带有详细信息的二维码图片,最终将所有二维码打包成ZIP文件供用户下载。
/**
     * 机构名称批量生成二维码下载
     *
     * @param request  请求对象
     * @param response 返回对象
     * @param ids      机构主键ID
     */
    @Override
    public void downloadQrCodeZip(HttpServletRequest request, HttpServletResponse response, String ids) {
        ZipOutputStream zos = null;
        try {
            // 查询机构名称
            List<Map<String, Object>> institutionList = qryInstitutionByIds(ids);
            if (!CommonUtil.isEmpty(institutionList) && institutionList.size() > 0) {
                //压缩包文件名称
                String downloadFilename = "活动机构扫描二维码";
                // 指明response的返回对象是文件流
                response.setContentType("application/octet-stream");
                // 设置在下载框默认显示的文件名
                String agent = request.getHeader("USER-AGENT");
                // 解决火狐浏览器下载文件中文名乱码问题
                if (agent != null && agent.toLowerCase().indexOf("firefox") > 0) {
                    downloadFilename = "=?UTF-8?B?" + (new String(Base64Util.encode(downloadFilename.getBytes("UTF-8")))) + "?=";
                    response.setHeader("Content-disposition", "attachment; filename=" + downloadFilename.concat(".zip"));
                }
                // 解决Safari浏览器下载文件中文名乱码问题
                else if (agent != null && agent.toLowerCase().indexOf("safari") > 0) {
                    response.setHeader("Content-disposition", "attachment; filename="
                            + (new String(downloadFilename.getBytes("UTF-8"), "ISO-8859-1")).concat(".zip"));
                } else {
                    response.setHeader("Content-disposition", "attachment; filename="
                            + URLEncoder.encode(downloadFilename.concat(".zip"), "UTF-8"));
                }
                zos = new ZipOutputStream(response.getOutputStream());
                for (Map<String, Object> map : institutionList) {
                    StringBuilder qrCode = new StringBuilder(""); // 扫描二维码图片获取对应机构编码
                    StringBuilder qrCodeName = new StringBuilder(""); // 二维码图片名称
                    // 开始拼接机构编码
                    qrCode.append((CommonUtil.isEmpty(map.get("companycode")) ? "##" : String.valueOf(map.get("companycode"))).concat("_")
                            + (CommonUtil.isEmpty(map.get("provincecode")) ? "##" : String.valueOf(map.get("provincecode"))).concat("_")
                            + (CommonUtil.isEmpty(map.get("citycode")) ? "##" : String.valueOf(map.get("citycode"))).concat("_")
                            + (CommonUtil.isEmpty(map.get("countycode")) ? "##" : String.valueOf(map.get("countycode"))).concat("_")
                            + (CommonUtil.isEmpty(map.get("sitecode")) ? "##" : String.valueOf(map.get("sitecode"))).concat("_")
                            + (CommonUtil.isEmpty(map.get("activitysession")) ? "##" : String.valueOf(map.get("activitysession"))).concat("_")
                            + map.get("activityid"));
                    // 开始拼接机构名称
                    if (!CommonUtil.isEmpty(map.get("companyname"))) {
                        qrCodeName.append(String.valueOf(map.get("companyname")).concat("_"));
                    }
                    if (!CommonUtil.isEmpty(map.get("provincename"))) {
                        qrCodeName.append(String.valueOf(map.get("provincename")).concat("_"));
                    }
                    if (!CommonUtil.isEmpty(map.get("cityname"))) {
                        qrCodeName.append(String.valueOf(map.get("cityname")).concat("_"));
                    }
                    if (!CommonUtil.isEmpty(map.get("countyname"))) {
                        qrCodeName.append(String.valueOf(map.get("countyname")).concat("_"));
                    }
                    if (!CommonUtil.isEmpty(map.get("sitename"))) {
                        qrCodeName.append(String.valueOf(map.get("sitename")).concat("_"));
                    }
                    if (!CommonUtil.isEmpty(map.get("activitysession"))) {
                        qrCodeName.append("场次" + String.valueOf(map.get("activitysession")).concat("_"));
                    }
                    qrCodeName = new StringBuilder(qrCodeName.substring(0, qrCodeName.length() - 1));
                    // 生成扫描机构二维码图片名称
                    zos.putNextEntry(new ZipEntry(qrCodeName + ".png"));
                    // 生成扫描机构二维码图片
                    getQrCodeImg(qrCode.toString(), zos);
                }
                zos.close();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriterException e) {
            e.printStackTrace();
        } finally {
            try {
                if (!CommonUtil.isEmpty(zos)) {
                    zos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                log.error("ZipOutputStream资源释放失败,原因:" + e.getMessage());
            }

        }
    }

    /**
     * 根据机构主键ID组查询机构名称集合
     *
     * @param ids
     * @return
     */
    private List<Map<String, Object>> qryInstitutionByIds(String ids) {
        try {
            // 拼装id主键条件参数
            String[] idArr = ids.split(",");
            ids = "";
            for (String id : idArr) {
                ids += id + ",";
            }
            ids = ids.substring(0, ids.length() - 1);
            // 查询脚本
            StringBuilder sb = new StringBuilder("SELECT " +
                    "    i.*," +
                    "    c.institutionname AS companyname," +
                    "    c1.institutionname provincename," +
                    "    c2.institutionname cityname," +
                    "    c3.institutionname countyname," +
                    "    c4.institutionname sitename " +
                    "FROM " +
                    "    institutionactivity i " +
                    "        LEFT JOIN " +
                    "    institutioncodetable c ON c.institutioncode = i.companycode " +
                    "        LEFT JOIN " +
                    "    institutioncodetable c1 ON c1.institutioncode = i.provincecode " +
                    "        LEFT JOIN " +
                    "    institutioncodetable c2 ON c2.institutioncode = i.citycode " +
                    "        LEFT JOIN " +
                    "    institutioncodetable c3 ON c3.institutioncode = i.countycode " +
                    "        LEFT JOIN " +
                    "    institutioncodetable c4 ON c4.institutioncode = i.sitecode " +
                    "WHERE " +
                    "    i.id IN (" + ids + ")");
//            List<Map<String, Object>> list = (List<Map<String, Object>>) institutionactivityDao
//                    .getList(sb.toString(), new Object[]{ids});
            List<Map<String, Object>> list = (List<Map<String, Object>>) institutionactivityDao
                    .getList(sb.toString());
            if (!CommonUtil.isEmpty(list) && list.size() > 0) {
                return list;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据机构主键ID组查询机构名称集合失败,原因:" + e.getMessage());
            return null;
        }
    }

    /**
     * 生成二维码
     *
     * @param content 扫描二维码显示内容
     * @param os      流
     * @throws WriterException
     * @throws IOException
     */
    private void getQrCodeImg(String content, OutputStream os) throws WriterException, IOException {
        //二维码参数
        int width = 200; // 图像宽度
        int height = 200; // 图像高度
        String format = "png";// 图像类型
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix;
        bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        MatrixToImageWriter.writeToStream(bitMatrix, format, os);
    }

 

转载于:https://www.cnblogs.com/xushuyi/articles/9002535.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值