国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

SpringBoot生成二維碼的實(shí)現(xiàn)

瀏覽:12日期:2023-04-04 17:41:51

效果圖

SpringBoot生成二維碼的實(shí)現(xiàn)

步驟

maven依賴

<!-- 生成二維碼--> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.0</version> </dependency>

工具類

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)試

SpringBoot生成二維碼的實(shí)現(xiàn)

線上調(diào)試

切換ip# 指定IP(防止ip轉(zhuǎn)發(fā)獲取的是內(nèi)網(wǎng)ip) file.upload.ip=127.0.0.1

到此這篇關(guān)于SpringBoot生成二維碼的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot生成二維碼內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 沙坪坝区| 贵州省| 厦门市| 乌兰察布市| 牙克石市| 彭泽县| 鸡西市| 烟台市| 远安县| 上饶市| 舟山市| 德阳市| 波密县| 东莞市| 大化| 大方县| 石林| 九江市| 合川市| 泰宁县| 阿勒泰市| 武宁县| 澄江县| 沾益县| 鄱阳县| 介休市| 佛学| 博湖县| 乌拉特前旗| 长丰县| 鲜城| 绥棱县| 淅川县| 毕节市| 祁东县| 英吉沙县| 汤原县| 烟台市| 武定县| 宜宾县| 宜章县|