| | |
| | | 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; |
| | |
| | | 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); |
| | | } |