From 4574296e4453f0cbb75a5fdb8bb39fa43c87297c Mon Sep 17 00:00:00 2001
From: Fancy <Fancy.fx@outlook.com>
Date: 星期四, 15 八月 2024 15:07:26 +0800
Subject: [PATCH] @param

---
 pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/QrCodeUtil.java |   33 +++++++++++++++++++++++++++++----
 1 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/QrCodeUtil.java b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/QrCodeUtil.java
index 93b5da3..4fae5bf 100644
--- a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/QrCodeUtil.java
+++ b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/QrCodeUtil.java
@@ -1,5 +1,6 @@
 package com.dy.pmsGlobal.util;
 
+import cn.hutool.core.codec.Base64;
 import com.alibaba.excel.EasyExcel;
 import com.alibaba.excel.converters.Converter;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
@@ -10,8 +11,7 @@
 import org.springframework.core.io.ClassPathResource;
 
 import java.awt.image.BufferedImage;
-import java.io.File;
-import java.io.IOException;
+import java.io.*;
 import java.net.URLEncoder;
 import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
@@ -31,8 +31,22 @@
     public static byte[] genQrCode(String code) throws IOException, WriterException {
         File logoFile = logoCache.computeIfAbsent(LOGO_PATH, key -> {
             try {
+                // 浣跨敤ClassPathResource鑾峰彇璧勬簮鐨勮緭鍏ユ祦
                 ClassPathResource resource = new ClassPathResource(key);
-                return resource.getFile();
+                // 鍒涘缓涓�涓复鏃舵枃浠舵潵淇濆瓨璧勬簮鍐呭(閬垮厤澶勭悊宓屽jar鏂囦欢鏃舵壘涓嶅埌鏂囦欢)
+                File tempFile = File.createTempFile("logo", ".png");
+                try (InputStream in = resource.getInputStream();
+                     OutputStream out = new FileOutputStream(tempFile)) {
+                    // 浠庤緭鍏ユ祦澶嶅埗鍒颁复鏃舵枃浠�
+                    byte[] buffer = new byte[1024];
+                    int bytesRead;
+                    while ((bytesRead = in.read(buffer)) != -1) {
+                        out.write(buffer, 0, bytesRead);
+                    }
+                    // 杩斿洖涓存椂鏂囦欢
+                    tempFile.deleteOnExit(); // 鑷姩鍒犻櫎涓存椂鏂囦欢锛堝綋JVM閫�鍑烘椂锛�
+                    return tempFile;
+                }
             } catch (IOException e) {
                 throw new RuntimeException("鍔犺浇Logo鍥剧墖澶辫触", e);
             }
@@ -45,6 +59,18 @@
         return QrCodeGenerator.bufferedImageToByteArray(qrCodeImage, "JPEG");
     }
 
+    public static String genQrCodeString(String code){
+        try {
+            byte[] codes = QrCodeUtil.genQrCode(code);
+            return "data:image/jpeg;base64," + Base64.encode(codes);
+        } catch (IOException e) {
+            e.printStackTrace();
+        } catch (WriterException e) {
+            e.printStackTrace();
+        }
+        return "";
+    }
+
     public static void downloadExcel(HttpServletResponse response, String fileName, String sheetName, List<Converter> list){
         if(CollectionUtils.isEmpty(list)){
             return;
@@ -55,7 +81,6 @@
             fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
             response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
             EasyExcel.write(response.getOutputStream(), list.get(0).getClass()).sheet(sheetName).doWrite(list);
-            EasyExcel.write(response.getOutputStream(), Converter.class).sheet(sheetName).doWrite(list);
         }catch (Exception e){
             log.error("瀵煎嚭浜у搧淇℃伅寮傚父", e);
         }

--
Gitblit v1.8.0