springboot+hutool如何批量生成二维码压缩导出功能
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇文章主要介绍springboot+hutool如何批量生成二维码压缩导出功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!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.批量图片压缩
单张图片输出或下载也不方便,这时候我们要用到压缩
批量导出压缩文件
代码如下
/** * 将文件打包成zip并下载 */ @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生成二维码压缩包并下载(不会在服务器生成文件)
代码如下:
/** * 图片内容与名字 */@Datapublic class QrImage { private byte[] bytes; private String name;}
/** * 将生成的二维码字节流压缩导出 * @param outputStream * @param qrImages */ public static void pngZip(OutputStream outputStream,ListqrImages) { //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(); } } }
/** * 将文件打包成zip并下载 */ @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
以上是"springboot+hutool如何批量生成二维码压缩导出功能"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
二维
二维码
生成
文件
内容
背景
图片
输出
功能
之间
代码
景色
灰色
篇文章
青色
颜色
测试
接下来
价值
兴趣
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络技术班服
深圳社交软件开发联系方式
数据库 用户管理
无线网络安全方向怎么样
联通服务器集采
django查看数据库
我的世界原版理想乡服务器
无人机 控制软件开发
企业网络安全绘画临摹
蜗牛学院网络安全工作
俄罗斯网络安全大赛
数据库在线设计工具
GDP增长率万德数据库查询
数据库查询结果拦截
唐山网络安全归哪个部门管理
微信登录服务器内部错误
投融资数据库有哪些
软件开发服务费面增值税吗
南阳网络安全工程师证
部队网络安全整改报告
洛阳科技职业学院互联网
自建存储服务器
在某个数据库中查询
电脑服务器配置价格
海信投屏无法连接服务器
sql数据库2010
事业单位网络安全风险
制图服务器
重庆程序软件开发价钱是多少
旅游app软件开发