springboot+hutool批量生成二维码压缩导出

springboot+hutool批量生成二维码压缩导出,第1张

springboot+hutool批量生成二维码压缩导出

文章目录
    • 1.引入依赖
    • 2.测试编码
    • 3.批量生成
    • 4.解析excel
    • 5.批量图片压缩
    • 6.上传excel直接将输出流转成压缩包

1.引入依赖
        
            com.google.zxing
            core
            3.4.1
        


        
            cn.hutool
            hutool-all
            5.7.14
        

2.测试编码
      QrConfig config = new QrConfig(300, 300);
// 设置边距,既二维码和背景之间的边距
        config.setMargin(3);
// 设置前景色,既二维码颜色(青色)
        config.setForeColor(Color.CYAN);
// 设置背景色(灰色)
        config.setBackColor(Color.GRAY);
// 生成二维码到文件,也可以到流
        QrCodeUtil.generate("12345678", config, FileUtil.file("E:/image/12345678.jpg"));


3.批量生成


然而我们要批量生成不可能完全靠手输,接下来实现导入excel表批量生成。
需要引入poi依赖:

4.解析excel
        
            org.apache.poi
            poi-ooxml
            5.0.0
        

准备好一份表格:

测试读取:

5.批量图片压缩

单张图片输出或下载也不方便,这时候我们要用到压缩

批量导出压缩文件


代码如下

 
    @PostMapping(value = "xiazai",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation("导出压缩包")
    public void download(HttpServletResponse response) throws IOException {
        response.setHeader("content-type", "application/octet-stream");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setContentType("application/octet-stream");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=download.zip");
        CompressUtil.createArchiver(CharsetUtil.CHARSET_UTF_8, ArchiveStreamFactory.ZIP,response.getOutputStream())
                .add(FileUtil.file("E:/image"));
//        ZipUtils.pngZip(response.getOutputStream(), qrImages);

    }
6.上传excel直接将输出流转成压缩包

尝试上传excel生成二维码压缩包并下载(不会在服务器生成文件)


代码如下:

@Data
public class QrImage {
    private byte[] bytes;

    private String name;
}

 
    public static void pngZip(OutputStream outputStream,List qrImages) {
        //Zip输出流
        ZipOutputStream zipOutputStream = null;
        try {
            zipOutputStream = new ZipOutputStream(outputStream);
            for (QrImage file : qrImages) {
                ZipEntry zipEntry = new ZipEntry(file.getName()+".png");
                zipOutputStream.putNextEntry(zipEntry);
                //写数据
                zipOutputStream.write(file.getBytes(), 0, file.getBytes().length);
                zipOutputStream.flush();
            }
            zipOutputStream.flush();
            zipOutputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                if (zipOutputStream != null) {
                    zipOutputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  
    @PostMapping(value = "xiazai",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiImplicitParam(name = "excelFile",value = "excel导入",required = true,dataType="MultipartFile",allowMultiple = true,paramType = "query")
    @ApiOperation("导出压缩包")
    public void download(@RequestParam("excelFile") MultipartFile file, HttpServletResponse response) throws IOException {
        //读取excel
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        List> lists = reader.read();
        //删除标题
        lists.remove(0);
        //批量生成二维码
        List qrImages = create(lists);
        response.setHeader("content-type", "application/octet-stream");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setContentType("application/octet-stream");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=download.zip");
//        CompressUtil.createArchiver(CharsetUtil.CHARSET_UTF_8, ArchiveStreamFactory.ZIP,response.getOutputStream())
//                .add(FileUtil.file("E:/image"));
        ZipUtils.pngZip(response.getOutputStream(), qrImages);

    }

    public List create(List> list) throws FileNotFoundException {
        QrConfig config = new QrConfig(300, 300);
// 设置边距,既二维码和背景之间的边距
        config.setMargin(3);
// 设置前景色,既二维码颜色(青色)
        config.setForeColor(Color.CYAN);
// 设置背景色(灰色)
        config.setBackColor(Color.GRAY);
        byte[] bytes=null;
        List qrImages = new ArrayList<>();
        QrImage qrImage;
// 生成二维码到文件,也可以到流
        for (List objects : list) {
            //将首列作为二维码内容
            qrImage = new QrImage();
            //将首列作为二维码内容
            bytes = QrCodeUtil.generatePng(objects.get(0).toString(),
                    config.setImg("E:/image/logo.png"));
            qrImage.setBytes(bytes);
            qrImage.setName(objects.get(0).toString());
            qrImages.add(qrImage);
        }

        return qrImages;
    }

					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4658576.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-06
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)