提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、导入依赖
  • 二、编写配置文件
  • 三、编写代码实现
    • 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 生成二维码相关推荐

  1. java 系统生成二维码实现扫码登录 springboot 生成二维码

    文章目录 前言 一.生成二维码 二.业务流程和代码逻辑梳理 总结 前言 使用框架  springboot  自己系统生成二维码,到前端网站,以及APP扫码登录流程,业务流程讲解梳理.也为自己做记录. ...

  2. SpringBoot生成二维码 扫描并可下载文件

    生成二维码 扫描并可下载文件 pom.xml 依赖 application.yaml Controller ImageBuilderUtils 工具类 适当根据自己的业务需求变通,然后就能轻松使用了, ...

  3. java springboot生成二维码图片

    java生成二维码图片 Maven依赖 <!--生成二维码--> <dependency><groupId>com.google.zxing</groupId ...

  4. Java生成二维码并把图片流导出压缩包下载(亲测可用)

    目录 背景 Maven依赖相关 二维码生成 基础实体类 二维码工具类 控制层代码 结果展示 单个二维码 二维码压缩包 源码地址 背景: 实际开发中有不少二维码生成并下载的需求,单个和批量下载都比较常见 ...

  5. SpringBoot Zxing _ Java 生成二维码(可内嵌图片)

    前提 jdk 要求:1.8:会 SpringBoot.Maven: 以下代码可以直接复制粘贴到项目中,可以直接使用~ 一.pom 准备 <?xml version="1.0" ...

  6. springboot+java生成二维码图片

    接下来将从IDEA创建springboot项目到生成效果图详细地为大家展示二维码的制作过程 1.首先是创建springboot项目 上面的图有红色标记的地方需要填写的,比如项目存放的路径,包名等,其他 ...

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

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

  8. SpringBoot实现二维码生成

    介绍 把字符串封装成二维码,扫码就可以获取字符串内容. 把网址封装成二维码,扫码就可以跳转到对应网页. 快速开始 导入依赖 <!--生成二维码--><dependency>&l ...

  9. SpringBoot利用ZXing工具来生成二维码(简单)

    一.简单二维码生成 1.1.依赖 <!--二维码工具--> <dependency><groupId>com.google.zxing</groupId> ...

最新文章

  1. mysql 脑裂的问题,DRBD脑裂问题故障处理
  2. APUE(第三章)文件IO
  3. 求数列的和 AC 杭电
  4. python散点图拟合曲线-python散点图:如何添加拟合线并显示拟合方程与R方?
  5. 忙了一上午终于把形状特征搞定了啊
  6. 很抱歉,博主 AFO 了
  7. 【今日CV 视觉论文速览】 04 Dec 2018
  8. Cordova+Ionic之坑
  9. hnu 暑期实训之回文串
  10. erlang 小程序:整数序列,搜索和为正的最长子序列
  11. python菜鸟编程-Python 基础教程 | 菜鸟教程
  12. 十年深圳人,是真的吗?
  13. 2017网易校招:不要二
  14. 10 16 进制 转换 c语言,求一段 16进制转10进制 C语言代码。 被转换的16进制数是 0x**型,转换后为10进制数。...
  15. 解决PyQt5程序报错Process finished with exit code -1073740791 (0xC0000409)
  16. php设计超级玛丽人物,面向对象实现简单版的超级马里奥小游戏
  17. ECCV2022|腾讯优图开源DisCo:拯救小模型在自监督学习中的效果
  18. 重走c语言—摸鱼大学生的c语言基础笔记
  19. 如何对互联网上产生的舆情传播动态进行分析的方法
  20. 跨境电商运营如何做好推特广告

热门文章

  1. 【最新】QQ机器人插件
  2. 大数据基础——HDFS(分布式文件系统)
  3. 【HR必看】Excel中对身份证号码的处理技巧
  4. 实现labelme批量json_to_dataset方法(anaconda)
  5. 叉乘点乘混合运算公式_用抽象指标记号推导nabla算符相关公式
  6. MATLAB - 拉普拉斯算子可视化
  7. Java读取串口数据
  8. 乌班图linux怎么连手机热点,ubuntu 16.04 设置位wifi热点 方法(手机可链接)亲测可用...
  9. HCIA-RoutingSwitching华为认证路由交换工程师(持续更新中2%)
  10. mysql md5 数据库_mysql数据库密码md5加密