java指定由若干url生成二维码png,然后打包为zip下载

额,又应项目要求,需要将客户配置的url生成二维码,然后打包为zip下载
上一篇是根据url生成图片打包下载,此篇仅仅多个根据url生成二维码
我沉默,话不多,赢的时候才开口.上代码

//公共方法根据url生成二维码图片后写入输出流里
    public static void getBarCodeImgByUrl(String url,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(url,BarcodeFormat.QR_CODE, width, height, hints);
        MatrixToImageWriter.writeToStream(bitMatrix, format, os);
    }

//入口
@RequestMapping("download")
    public void download(HttpServletRequest request, HttpServletResponse response,BcMerchantAccount userInfo,String identy){

            //通过活动标识和商户id查询活动
            List<info> infoList = xxService.getInfoList(XX,XX);
            if(infoList != null && infoList.size()>0){
                ZipOutputStream zos = null;
                try {
                    String downloadFilename = infoList.get(0).getfileName();//文件的名称
                    downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
                    response.setContentType("application/octet-stream");// 指明response的返回对象是文件流 
                    response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename+".zip");// 设置在下载框默认显示的文件名
                    zos = new ZipOutputStream(response.getOutputStream());
                    for(info info:infoList ){
                        zos.putNextEntry(new ZipEntry(info.getBarCode_name()+".png"));//命名
                        getBarCodeImgByUrl(info.getUrl, zos);//拼接了url
                    }
                    zos.flush();     
                    zos.close();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (WriterException e) {
                    e.printStackTrace();
                } finally{
                    if(zos != null){
                        try {
                            zos.flush();
                            zos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }     

                    }

                }
            }


    }
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值