diff --git a/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java b/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java index 8a4ca8f..94e56fd 100644 --- a/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java +++ b/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java @@ -24,9 +24,9 @@ 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); + String icon_url = QRCodeUtil.downloadPicture(collect.getIcon_url()); 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 d462fcb..944b46f 100644 --- a/src/main/java/com/lovenav/utils/QRCodeUtil.java +++ b/src/main/java/com/lovenav/utils/QRCodeUtil.java @@ -18,11 +18,8 @@ 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; -import java.util.Random; -import java.util.Base64; +import java.text.SimpleDateFormat; +import java.util.*; @Component @Service @@ -386,44 +383,52 @@ public class QRCodeUtil { /** * 根据图片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(); + + public static String downloadPicture(String pictureurl) throws Exception { + //因为是测试,图片url可以随便搞一个,我从百度随便复制了个图片url + String pictureUrl = pictureurl; + //建立图片连接 + URL url = new URL(pictureUrl); + HttpURLConnection connection = (HttpURLConnection)url.openConnection(); + //设置请求方式 connection.setRequestMethod("GET"); - connection.connect(); + //设置超时时间 + connection.setConnectTimeout(10*1000); - int responseCode = connection.getResponseCode(); - if (responseCode == HttpURLConnection.HTTP_OK) { - BufferedInputStream in = new BufferedInputStream(connection.getInputStream()); - FileOutputStream out = new FileOutputStream(new File(savePath)); + //输入流 + InputStream stream = connection.getInputStream(); + int len = 0; + 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(); - out.close(); - connection.disconnect(); - System.out.println("图片下载成功,保存路径:" + savePath); -// 返回本地地址 - return savePath+"/"+file; + //获取项目路径 + File directory = new File("src/main/resources/static/icon"); + String paths = directory.getCanonicalPath(); + //如果没有文件夹则创建 + File file = new File("src/main/resources/static/icon"); + if (!file.exists()){ + 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 { diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 332ae23..c08f7aa 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,4 +1,6 @@ spring: + mvc: + static-path-pattern: /static/** datasource: url: jdbc:mysql://localhost:3306/love-nav username: root