update QRCode

This commit is contained in:
sjm 2023-12-24 01:59:52 +08:00
parent ac97a3b69a
commit 9985a9f81a
3 changed files with 45 additions and 38 deletions

View File

@ -24,9 +24,9 @@ public class QRCServiceImpl implements QRCService{
public String QR(CollectIconList collect) throws Exception { public String QR(CollectIconList collect) throws Exception {
String logoPath ="src/main/resources/static/logo/NAV.png"; String logoPath ="src/main/resources/static/logo/NAV.png";
String destPath = "src/main/resources/static/qr"; String destPath = "src/main/resources/static/qr";
String savePath = "src/main/resources/static/icon";
// 将icon下载到本地并存储本地路径 // 将icon下载到本地并存储本地路径
String icon_url = QRCodeUtil.downloadPicture(collect.getIcon_url(),savePath); String icon_url = QRCodeUtil.downloadPicture(collect.getIcon_url());
collect.setIcon_url(icon_url); collect.setIcon_url(icon_url);
// 通过传入collect的url_id查找相同网址图片列表 // 通过传入collect的url_id查找相同网址图片列表
// List<CollectIconList> collectIconList = collectIconListDao.selectByUrlid(collect.getUrl_id()); // List<CollectIconList> collectIconList = collectIconListDao.selectByUrlid(collect.getUrl_id());

View File

@ -18,11 +18,8 @@ import java.awt.image.BufferedImage;
import java.io.*; import java.io.*;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.HashMap; import java.text.SimpleDateFormat;
import java.util.Hashtable; import java.util.*;
import java.util.Map;
import java.util.Random;
import java.util.Base64;
@Component @Component
@Service @Service
@ -386,44 +383,52 @@ public class QRCodeUtil {
/** /**
* 根据图片url下载图片到本地 * 根据图片url下载图片到本地
* @param imageUrl
* @param savePath
* @throws IOException
*/ */
public static String downloadPicture(String imageUrl, String savePath) throws IOException {
URL url = new URL(imageUrl); public static String downloadPicture(String pictureurl) throws Exception {
//因为是测试图片url可以随便搞一个我从百度随便复制了个图片url
String pictureUrl = pictureurl;
//建立图片连接
URL url = new URL(pictureUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//设置请求方式
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
connection.connect(); //设置超时时间
connection.setConnectTimeout(10*1000);
int responseCode = connection.getResponseCode(); //输入流
if (responseCode == HttpURLConnection.HTTP_OK) { InputStream stream = connection.getInputStream();
BufferedInputStream in = new BufferedInputStream(connection.getInputStream()); int len = 0;
FileOutputStream out = new FileOutputStream(new File(savePath)); byte[] test = new byte[1024];
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(); File directory = new File("src/main/resources/static/icon");
out.close(); String paths = directory.getCanonicalPath();
connection.disconnect(); //如果没有文件夹则创建
System.out.println("图片下载成功,保存路径:" + savePath); File file = new File("src/main/resources/static/icon");
// 返回本地地址 if (!file.exists()){
return savePath+"/"+file; file.mkdirs();
} }
else {
System.out.println("无法下载图片,响应码:" + responseCode); //设置图片名称这个随意
return JSON.toJSONString("responseCode"); String date = new Random().nextInt(99999999)+".jpg";
String fileName = date ;
//输出流图片输出的目的文件
String serverPath = "src/main/resources/static/icon";
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(serverPath +"/" + fileName));
//以流的方式上传
while ((len =stream.read(test)) !=-1){
fos.write(test,0,len);
} }
//记得关闭流不然消耗资源
stream.close();
fos.close();
return (serverPath +"/" + fileName);
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {

View File

@ -1,4 +1,6 @@
spring: spring:
mvc:
static-path-pattern: /static/**
datasource: datasource:
url: jdbc:mysql://localhost:3306/love-nav url: jdbc:mysql://localhost:3306/love-nav
username: root username: root