springboot 生成二维码
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、导入依赖
- 二、编写配置文件
- 三、编写代码实现
- 1 controller层编写
- 2 Constant编写
- 3 映射规则编写(出于安全考虑不直接暴露内部路径)
- 4 测试
前言
如何使用springboot 生成二维码呢?
提示:以下是本篇文章正文内容,下面案例可供参考
一、导入依赖
<!-- 生成二维码用--><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.0</version></dependency>
二、编写配置文件
主要看fie的配置 一会儿会用到
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: 123456url: jdbc:mysql://localhost:3306/imooc_mall?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8mvc:pathmatch:matching-strategy: ant_path_matcher #to use old Swaggerredis:host: localhostport: 6379password:main:allow-circular-references: true # to use PageHelper : allow circular-references
server:port: 8084
mybatis:mapper-locations: classpath:mappers/*.xml #where mybatis.xml isfile:upload:dir: E:/mall/file/ip: 127.0.0.1dir: /root/data/code_img/ip: xxxxxxx
三、编写代码实现
1 controller层编写
没有service层
controller直接实现了
package com.example.sky.util;import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;/*** 描述: 生成二维码工具*/
public class QRCodeGenerator {public static void generateQRCodeImage(String text, int width, int height, String filePath)throws WriterException, IOException {//声明二维码写出者QRCodeWriter qrCodeWriter = new QRCodeWriter();BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);Path path = FileSystems.getDefault().getPath(filePath);MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);}// public static void main(String[] args) {// try {// generateQRCodeImage("Hello World", 350, 350, "/Users/didi/Desktop/IdeaProjects/imooc-mall-prepare-static/QRTest.png");
// } catch (WriterException e) {// e.printStackTrace();
// } catch (IOException e) {// e.printStackTrace();
// }
// }
}
package com.example.erweimademo.controller;import com.example.erweimademo.Constant;
import com.example.erweimademo.utils.QRCodeGenerator;
import com.google.zxing.WriterException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;
import java.io.IOException;@RestController
public class testController {@Value("${file.upload.ip}")String ip;@RequestMapping("/test")public String test(@RequestParam String data){ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest();String address = ip + ":" + request.getLocalPort();try {QRCodeGenerator.generateQRCodeImage(data, 350, 350,Constant.FILE_UPLOAD_DIR + data + ".png");} catch (WriterException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}//前端访问这个地址会被mvc重定向 从而访问到系统内部的文件String pngAddress = "http://" + address + "/images/" + data + ".png";return pngAddress;}}
2 Constant编写
参考结构
public static String FILE_UPLOAD_DIR;@Value("${file.upload.dir}")public void setFileUploadDir(String fileUploadDir) {FILE_UPLOAD_DIR = fileUploadDir;}
3 映射规则编写(出于安全考虑不直接暴露内部路径)
参考结构
package com.imooc.mall.config;import com.imooc.mall.common.Constant;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/*** 描述: 配置地址映射*/
@Configuration
public class ImoocMallWebMvcConfig implements WebMvcConfigurer {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/images/**").addResourceLocations("file:" + Constant.FILE_UPLOAD_DIR);}
}
4 测试
我们访问return的url 成功访问到本地的二维码文件
springboot 生成二维码相关推荐
- java 系统生成二维码实现扫码登录 springboot 生成二维码
文章目录 前言 一.生成二维码 二.业务流程和代码逻辑梳理 总结 前言 使用框架 springboot 自己系统生成二维码,到前端网站,以及APP扫码登录流程,业务流程讲解梳理.也为自己做记录. ...
- SpringBoot生成二维码 扫描并可下载文件
生成二维码 扫描并可下载文件 pom.xml 依赖 application.yaml Controller ImageBuilderUtils 工具类 适当根据自己的业务需求变通,然后就能轻松使用了, ...
- java springboot生成二维码图片
java生成二维码图片 Maven依赖 <!--生成二维码--> <dependency><groupId>com.google.zxing</groupId ...
- Java生成二维码并把图片流导出压缩包下载(亲测可用)
目录 背景 Maven依赖相关 二维码生成 基础实体类 二维码工具类 控制层代码 结果展示 单个二维码 二维码压缩包 源码地址 背景: 实际开发中有不少二维码生成并下载的需求,单个和批量下载都比较常见 ...
- SpringBoot Zxing _ Java 生成二维码(可内嵌图片)
前提 jdk 要求:1.8:会 SpringBoot.Maven: 以下代码可以直接复制粘贴到项目中,可以直接使用~ 一.pom 准备 <?xml version="1.0" ...
- springboot+java生成二维码图片
接下来将从IDEA创建springboot项目到生成效果图详细地为大家展示二维码的制作过程 1.首先是创建springboot项目 上面的图有红色标记的地方需要填写的,比如项目存放的路径,包名等,其他 ...
- springboot+hutool批量生成二维码压缩导出
文章目录 1.引入依赖 2.测试编码 3.批量生成 4.解析excel 5.批量图片压缩 6.上传excel直接将输出流转成压缩包 1.引入依赖 <!-- 生成二维码依赖--><de ...
- SpringBoot实现二维码生成
介绍 把字符串封装成二维码,扫码就可以获取字符串内容. 把网址封装成二维码,扫码就可以跳转到对应网页. 快速开始 导入依赖 <!--生成二维码--><dependency>&l ...
- SpringBoot利用ZXing工具来生成二维码(简单)
一.简单二维码生成 1.1.依赖 <!--二维码工具--> <dependency><groupId>com.google.zxing</groupId> ...
最新文章
- Expandable Table的Demo
- 模拟人类医生,自动生成靠谱医学报告,腾讯医典创新方法入选CVPR 2021
- NVIDIA Jetson Xavier NX中安装的python库包的版本
- mysql索引检测_mysql检测重复索引
- kotlin学习之对象(九)
- (转载)windows server 2003的注意事项
- 程序员必备软技能之科技趋势(一)
- Linux服务器添加SVN用户
- windows远程桌面无法粘贴复制的问题解决方法
- Unity Ragdoll 实现死亡效果 心得+坑点总结
- 开始学习编写用于 Windows SideShow 设备的小工具【转】
- 思科简单教程CCNA
- 查看linux系统版本命令大全
- 时间序列的平稳性检验方法汇总
- [转帖]VBS 教程
- 第三方可视化数据分析图表Pyecharts(下载保存图片(生成的html图片)、zip函数(将数据转换为列表加元组的格式)、南丁格尔玫瑰图、双y轴可视化、饼形图和环形图)
- linux怎么生成arm文件,AMR 文件扩展名: 它是什么以及如何打开它?
- 信息安全实验:标准IP的ACLs的配置(cisco模拟器)
- zigbee3.0 ota 实验
- 原厂对NPI安全稽核要求
热门文章
- 纯CSS 写动画背景,高仿蚂蚁庄园小鸡仔
- bash: vi: command not found
- ble 读写特征值特征值_BLE添加特征值
- python毕业设计能做什么工作_用python可以做什么毕业设计项目|融资公司的主要业务...
- html怎么设计关键字,干货分享——关键词如何做标记
- 【技术文档】centernet(姿态估计)
- centos7/win7 双系统安装教程
- 洛谷4208 最小生成树计数
- jzoj(senior)4208. 【五校联考1day1】线段树什么的最讨厌了
- Centos 更换静态IP脚本