diff --git a/src/main/java/com/lovenav/controller/UrlAndCateController.java b/src/main/java/com/lovenav/controller/UrlAndCateController.java index 1f267c4..1704e9f 100644 --- a/src/main/java/com/lovenav/controller/UrlAndCateController.java +++ b/src/main/java/com/lovenav/controller/UrlAndCateController.java @@ -102,85 +102,62 @@ public class UrlAndCateController { { List urlCateLists = urlCateListService.selectUrListByUserId(Integer.valueOf(userId)); List urlLists = urlListService.selectUrList(); - List cateAndUrlList = new ArrayList<>(); - - - - - //预处理为CateAndUrl对象 - for( int i = 0 ; i < urlCateLists.size() ; i++) - { + for (UrlCateList urlCateList : urlCateLists) { CateAndUrl cateAndUrl = new CateAndUrl(); cateAndUrl.setFloder("true"); - cateAndUrl.setName(urlCateLists.get(i).getName()); - cateAndUrl.setParentId(urlCateLists.get(i).getRootCateId()); - cateAndUrl.setCateId(urlCateLists.get(i).getId()); + cateAndUrl.setName(urlCateList.getName()); + cateAndUrl.setParentId(urlCateList.getRootCateId()); + cateAndUrl.setCateId(urlCateList.getId()); cateAndUrlList.add(cateAndUrl); - } - for( int i = 0 ; i < urlLists.size() ; i++) - { - CateAndUrl cateAndUrl = new CateAndUrl(); - cateAndUrl.setFloder("false"); - cateAndUrl.setName(urlLists.get(i).getName()); - cateAndUrl.setParentId(urlLists.get(i).getCateId()); - cateAndUrl.setUrlId(urlLists.get(i).getId()); - cateAndUrlList.add(cateAndUrl); - - } - - List parentsList = new ArrayList<>(); - //声明返回集合 - List resultList = new ArrayList<>(); - //对全部数据进行遍历 - for (CateAndUrl disease : cateAndUrlList) { - //判断是否是第一梯队,或者说是树的根节点,如果是根节点就加入到父类集合与返回集合 - if (disease.getParentId() == 0) { - parentsList.add(disease); - resultList.add(disease); - } else { - //对于不是第一梯队的数据进行遍历 - for (CateAndUrl parent : parentsList) { - //对数据的pid与父类集合中的父节点进行配对,如果配对成功,就把数据加入到父节点中的子节点集合 - - if (parent.getFloder().equals("true") ) { - if( parent.getCateId() == disease.getParentId()) - { - parent.getChildUC().add(disease); - //当前数据有可能是别的数据的父节点,加到父类容器 - parentsList.add(disease); - break; - } - - } - if (parent.getFloder().equals("false") ) { - if(parent.getUrlId() == disease.getParentId()) - { - parent.getChildUC().add(disease); - //当前数据有可能是别的数据的父节点,加到父类容器 - parentsList.add(disease); - break; - } - - } + for( int i = 0 ; i < urlLists.size() ; i++){ + UrlList urlList = urlLists.get(i); + for(CateAndUrl cateAndUrl1 : cateAndUrlList) { + if (urlList.getCateId() == cateAndUrl1.getCateId()) + { + CateAndUrl cateAndUrl = new CateAndUrl(); + cateAndUrl.setFloder("false"); + cateAndUrl.setName(urlList.getName()); + cateAndUrl.setParentId(urlList.getCateId()); + cateAndUrl.setUrlId(urlList.getId()); + cateAndUrl.setUrl(urlList.getUrl()); + cateAndUrl.setIcon(urlList.getIcon()); + cateAndUrl.setCreatetime(urlList.getCreatetime()); + cateAndUrl.setViews(urlList.getViews()); + cateAndUrl.setTags(urlList.getTags()); + cateAndUrl.setWeigh(urlList.getWeigh()); + cateAndUrl.setStatus(urlList.getStatus()); + cateAndUrl.setDesc(urlList.getDesc()); + cateAndUrl.setNeedLogin(urlList.getNeedLogin()); + cateAndUrl.setAgentHint(urlList.getAgentHint()); + cateAndUrl.setIsNeedAgent(urlList.getIsNeedAgent()); + cateAndUrl.setIsAd(urlList.getIsAd()); + cateAndUrl.setIsTop(urlList.getIsTop()); + cateAndUrl.setIsEncrypt(urlList.getIsEncrypt()); + cateAndUrl1.getChildUC().add(cateAndUrl); + break; } } - } + + + + } ObjectMapper mapper = new ObjectMapper(); - String Json; - HashMap result = new HashMap<>(); - // java对象转换为json字符换 + String result = null; try { - Json = mapper.writeValueAsString(resultList); - result.put("code", 200); - result.put("msg", "处理成功"); - result.put("data", resultList); + HashMap data = new HashMap<>(); + data.put("code", 200); + data.put("msg", "处理成功"); + data.put("data", cateAndUrlList); + result = mapper.writeValueAsString(cateAndUrlList); } catch (JsonProcessingException e) { throw new RuntimeException(e); } - return JSONObject.toJSONString(result); + + return result; + } @RequestMapping("/disposeBookmarkExhibitedToJson") diff --git a/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java b/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java index b7c1c8e..b4b1373 100644 --- a/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java +++ b/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java @@ -29,21 +29,32 @@ public class QRCServiceImpl implements QRCService{ // 通过collect的url_id查找网址id对应网址 UrlList urlList = urlListDao.selectByPrimaryKey(Long.valueOf(collect.getUrl_id())); - if(urlList != null){ + + List list = collectIconListDao.selectByUrlid(collect.getUrl_id()); + if(list.size()==0){ + if(urlList != null){ +// 将icon下载到本地并存储本地路径 + String icon_url = QRCodeUtil.downloadPicture(collect.getIcon_url()); + collect.setIcon_url(icon_url); // 获取网址url - String url = urlList.getUrl(); + String url = urlList.getUrl(); // 将网址生成二维码并返回本地路径 - String url_wait = QRCodeUtil.encode(url, logoPath, destPath, true); + String url_wait = QRCodeUtil.encode(url, logoPath, destPath, true); // 为collect设置二维码本地路径 - collect.setQr_url(url_wait); - collectIconListDao.insert(collect); - String base64 = QRCodeUtil.ImageToBase64(url_wait); - return base64; + collect.setQr_url(url_wait); + collectIconListDao.insert(collect); + String base64 = QRCodeUtil.ImageToBase64(url_wait); + return JSON.toJSONString(base64); + }else{ + HashMap result = new HashMap<>(); + result.put("code",500); + result.put("msg", "找不到对应网址"); + return JSON.toJSONString(result); + } }else{ - HashMap result = new HashMap<>(); - result.put("code",500); - result.put("msg", "找不到对应网址"); - return JSON.toJSONString(result); + String url_wait = list.get(0).getQr_url(); + String base64 = QRCodeUtil.ImageToBase64(url_wait); + return JSON.toJSONString(base64); } } } diff --git a/src/main/java/com/lovenav/utils/QRCodeUtil.java b/src/main/java/com/lovenav/utils/QRCodeUtil.java index 57c0896..944b46f 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,11 +16,10 @@ import java.awt.*; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.*; -import java.util.HashMap; -import java.util.Hashtable; -import java.util.Map; -import java.util.Random; -import java.util.Base64; +import java.net.HttpURLConnection; +import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.*; @Component @Service @@ -381,13 +381,62 @@ public class QRCodeUtil { return encoder.encodeToString(data); } + /** + * 根据图片url下载图片到本地 + */ + + 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.setConnectTimeout(10*1000); + + //输入流 + InputStream stream = connection.getInputStream(); + int len = 0; + byte[] test = new byte[1024]; + + + //获取项目路径 + 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(); + } + + //设置图片名称,这个随意 + 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 { - 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); diff --git a/src/main/java/com/lovenav/utils/UrlCheckUtil.java b/src/main/java/com/lovenav/utils/UrlCheckUtil.java index 6709e40..b0f8635 100644 --- a/src/main/java/com/lovenav/utils/UrlCheckUtil.java +++ b/src/main/java/com/lovenav/utils/UrlCheckUtil.java @@ -1,4 +1,7 @@ package com.lovenav.utils; +import java.io.BufferedReader; + +import com.alibaba.fastjson2.JSON; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; @@ -6,6 +9,7 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.IOException; +import java.io.InputStreamReader; import java.net.*; import javax.net.ssl.HttpsURLConnection; @@ -33,6 +37,10 @@ public class UrlCheckUtil { return true; } + /** + * 判断链接是否有效 + * 输入链接 + */ public static String checkUrlConnection(String url) { // 创建http POST请求 HttpGet httpGet = new HttpGet(url); @@ -60,6 +68,9 @@ public class UrlCheckUtil { return String.valueOf(statusCode); } + /** + * 检查http连接 + */ public static boolean CheckHttp(String address) throws URISyntaxException, MalformedURLException { URL url = new URL(address); URI uri = url.toURI(); @@ -70,4 +81,46 @@ public class UrlCheckUtil { return false; } -} \ No newline at end of file + /** + * 发送GET请求 + */ + public static String sendGetRequest(String url) throws IOException { + URL requestUrl = new URL(url); + HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection(); + connection.setRequestMethod("GET"); + + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder response = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + response.append(line); + } + reader.close(); + return response.toString(); + } else { + throw new RuntimeException("HTTP GET request failed with response code: " + responseCode); + } + } + + public static String url_speed(String url) { + String url_check = url; + int numRequests = 10; + long totalResponseTime = 0; + + for (int i = 0; i < numRequests; i++) { + long startTime = System.currentTimeMillis(); + try { + UrlCheckUtil.sendGetRequest(url_check); + long endTime = System.currentTimeMillis(); + long responseTime = endTime - startTime; + totalResponseTime += responseTime; + } catch (IOException e) { + e.printStackTrace(); + } + } + double averageResponseTime = (double) totalResponseTime / (1 * numRequests); + return JSON.toJSONString(averageResponseTime + " ms"); + } +} 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