SpringBoot生成二維碼,怎么搞?
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”
優(yōu)質(zhì)文章,第一時(shí)間送達(dá)
66套java從入門到精通實(shí)戰(zhàn)課程分享
效果圖
步驟
maven依賴
????????
????????????com.google.zxing
????????????javase
????????????3.3.0
????????
工具類
package?com.bennyrhys.mall.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,?"E:/JAVA/mall/src/main/resources/images/QRTest.png");
????????}?catch?(WriterException?e)?{
????????????e.printStackTrace();
????????}?catch?(IOException?e)?{
????????????e.printStackTrace();
????????}
????}
}
service
???/**
?????*?生成二維碼
?????*?圖片可解析出訪問(wèn)的支付對(duì)應(yīng)訂單號(hào)的支付連接
?????*?@param?orderNo?訂單號(hào)
?????*?@return?返回圖片地址
?????*/
????@Override
????public?String?qrcode(String?orderNo)?{
????????ServletRequestAttributes?attributes?=?(ServletRequestAttributes)?RequestContextHolder.getRequestAttributes();
????????HttpServletRequest?request?=?attributes.getRequest();
????????String?address?=?ip?+?":"?+?request.getLocalPort();
????????String?payUrl?=?"http://"?+?address?+?"/pay?orderNo="?+?orderNo;
????????try?{
????????????QRCodeGenerator.generateQRCodeImage(payUrl,?350,?350,?Constant.FILE_UPLOAD_PATH?+?orderNo?+?".png");
????????}?catch?(WriterException?e)?{
????????????e.printStackTrace();
????????}?catch?(IOException?e)?{
????????????e.printStackTrace();
????????}
????????String?pngAddress?=?"http://"?+?address?+?"/images-dev/"?+?orderNo?+?".png";
????????return?pngAddress;
????}
擴(kuò)展
局域網(wǎng)調(diào)試? ?

線上調(diào)試
切換ip# 指定IP(防止ip轉(zhuǎn)發(fā)獲取的是內(nèi)網(wǎng)ip) file.upload.ip=127.0.0.1
版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接和本聲明。
本文鏈接:
https://blog.csdn.net/weixin_43469680/article/details/110791319
粉絲福利:實(shí)戰(zhàn)springboot+CAS單點(diǎn)登錄系統(tǒng)視頻教程免費(fèi)領(lǐng)取
???
?長(zhǎng)按上方微信二維碼?2 秒 即可獲取資料
感謝點(diǎn)贊支持下哈?
評(píng)論
圖片
表情
