From ac97a3b69a46115594c29c20fc7f3b742bd50e03 Mon Sep 17 00:00:00 2001 From: sjm <2431685932@qq.com> Date: Sat, 23 Dec 2023 23:59:08 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BA=8C=E7=BB=B4=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/serviceImpl/QRCServiceImpl.java | 4 ++ .../java/com/lovenav/utils/QRCodeUtil.java | 54 +++++++++++++++++-- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java b/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java index b7c1c8e..8a4ca8f 100644 --- a/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java +++ b/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java @@ -24,6 +24,10 @@ public class QRCServiceImpl implements QRCService{ public String QR(CollectIconList collect) throws Exception { String logoPath ="src/main/resources/static/logo/NAV.png"; String destPath = "src/main/resources/static/qr"; + String savePath = "src/main/resources/static/icon"; +// 将icon下载到本地并存储本地路径 + String icon_url = QRCodeUtil.downloadPicture(collect.getIcon_url(),savePath); + collect.setIcon_url(icon_url); // 通过传入collect的url_id查找相同网址图片列表 // List collectIconList = collectIconListDao.selectByUrlid(collect.getUrl_id()); diff --git a/src/main/java/com/lovenav/utils/QRCodeUtil.java b/src/main/java/com/lovenav/utils/QRCodeUtil.java index 57c0896..d462fcb 100644 --- a/src/main/java/com/lovenav/utils/QRCodeUtil.java +++ b/src/main/java/com/lovenav/utils/QRCodeUtil.java @@ -1,5 +1,6 @@ package com.lovenav.utils; +import com.alibaba.fastjson.JSON; import com.google.zxing.*; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.BitMatrix; @@ -15,6 +16,8 @@ import java.awt.*; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; @@ -381,13 +384,54 @@ public class QRCodeUtil { return encoder.encodeToString(data); } + /** + * 根据图片url下载图片到本地 + * @param imageUrl + * @param savePath + * @throws IOException + */ + public static String downloadPicture(String imageUrl, String savePath) throws IOException { + URL url = new URL(imageUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.connect(); + + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + BufferedInputStream in = new BufferedInputStream(connection.getInputStream()); + FileOutputStream out = new FileOutputStream(new File(savePath)); + + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = in.read(buffer)) != -1) { + out.write(buffer, 0, bytesRead); + } +// 能够返回本地地址前提 + URL url2 = new URL(imageUrl); + BufferedImage image = ImageIO.read(url2); + String file = new Random().nextInt(99999999)+".jpg"; + ImageIO.write(image, FORMAT_NAME, new File(savePath+"/"+file)); + +// 下载完成,关闭输入输出流 + in.close(); + out.close(); + connection.disconnect(); + System.out.println("图片下载成功,保存路径:" + savePath); +// 返回本地地址 + return savePath+"/"+file; + } + else { + System.out.println("无法下载图片,响应码:" + responseCode); + return JSON.toJSONString("responseCode"); + } + } public static void main(String[] args) throws Exception { - String text = "https://www.baidu.com"; //这里设置自定义网站url - String logoPath ="src/main/resources/static/logo/NAV.png"; - String destPath = "src/main/resources/static/qr"; - String url = QRCodeUtil.encode(text, logoPath, destPath, true); - QRCodeUtil.ImageToBase64("src/main/resources/static/qr/44672405.jpg"); +// String text = "https://www.baidu.com"; //这里设置自定义网站url +// String logoPath ="src/main/resources/static/logo/NAV.png"; +// String destPath = "src/main/resources/static/qr"; +// String url = QRCodeUtil.encode(text, logoPath, destPath, true); +// QRCodeUtil.ImageToBase64("src/main/resources/static/qr/44672405.jpg"); // System.out.println(url); // String base64 = getBase64(url); // System.out.println(base64);