Browse Source

提交保存

xusonglin 3 years ago
parent
commit
75f345288b

+ 1 - 1
pom.xml

@@ -10,7 +10,7 @@
     </parent>
     <groupId>com.jkcredit.info.query</groupId>
     <artifactId>info-query</artifactId>
-    <version>1.0.11</version>
+    <version>1.0.12</version>
     <name>info-query</name>
     <description>Demo project for Spring Boot</description>
     <properties>

+ 1 - 1
src/main/java/com/jkcredit/info/query/InfoQueryApplication.java

@@ -7,7 +7,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
 
 @EnableEurekaClient
 @SpringBootApplication
-@EnableScheduling
+//@EnableScheduling
 public class InfoQueryApplication {
 
     public static void main(String[] args) {

+ 26 - 0
src/main/java/com/jkcredit/info/query/config/WebMvcConfig.java

@@ -0,0 +1,26 @@
+package com.jkcredit.info.query.config;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.converter.BufferedImageHttpMessageConverter;
+import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+import java.util.List;
+
+/**
+ * @author xusonglin
+ * @version V1.0
+ **/
+@Configuration
+public class WebMvcConfig implements WebMvcConfigurer {
+
+    /**
+     * 增加图片转换器
+     * @param converters
+     */
+    @Override
+    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
+        converters.add(new BufferedImageHttpMessageConverter());
+    }
+
+}

+ 45 - 0
src/main/java/com/jkcredit/info/query/controller/InfoQueryController.java

@@ -5,15 +5,26 @@ import com.jkcredit.common.model.CommonResponseObject;
 import com.jkcredit.info.query.service.InformationQueryService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.cloud.client.discovery.DiscoveryClient;
 import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.net.URL;
+
 @RestController
 @Slf4j
 public class InfoQueryController {
+    @Value("${supplier.hostUrl}")
+    private String hostUrl;
+
     @Autowired
     InformationQueryService informationQueryService;
 
@@ -37,6 +48,40 @@ public class InfoQueryController {
         return informationQueryService.queryDetail(body.getString("path"));
     }
 
+    @GetMapping(value = "/getImage", produces = {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE})
+    public BufferedImage getImage(String exportType, String gmsfhm) {
+        StringBuffer urlStringBuffer = new StringBuffer(hostUrl);
+        urlStringBuffer.append("photoSearch/singlePhotoSearch.action?exportType=").append(exportType);
+        urlStringBuffer.append("&gmsfhm=").append(gmsfhm);
+        String imageUrl = urlStringBuffer.toString();
+        log.info("getImage.imageUrl:{}", imageUrl);
+        try {
+            return ImageIO.read(new URL(imageUrl));
+        } catch (Exception e) {
+            log.error("getImage.url:{},Exception:{}", urlStringBuffer.toString(), e);
+        }
+        return null;
+    }
+
+    // ../crjPhotoSearch/singlePhotoSearch.action?exportType=img&zjhm=C21368872&csdgj=中国&jgmc=河南省荥阳市
+    @GetMapping(value = "/getCrjImage", produces = {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE})
+    public BufferedImage getCrjImage(String exportType, String zjhm, String csdgj, String jgmc) {
+        StringBuffer urlStringBuffer = new StringBuffer(hostUrl);
+        urlStringBuffer.append("crjPhotoSearch/singlePhotoSearch.action?exportType=").append(exportType);
+        urlStringBuffer.append("&zjhm=").append(zjhm);
+        urlStringBuffer.append("&csdgj=").append(csdgj);
+        urlStringBuffer.append("&jgmc=").append(jgmc);
+        String imageUrl = urlStringBuffer.toString();
+        log.info("getCrjImage.imageUrl:{}", imageUrl);
+        try {
+            return ImageIO.read(new URL(imageUrl));
+        } catch (Exception e) {
+            log.error("getImage.url:{},Exception:{}", urlStringBuffer.toString(), e);
+        }
+        return null;
+    }
+
+
     @PostMapping("/loadCookie")
     public CommonResponseObject loadCookie() {
         return informationQueryService.loadCookie();

+ 68 - 6
src/main/java/com/jkcredit/info/query/service/impl/InformationQueryServiceImpl.java

@@ -8,6 +8,7 @@ import com.jkcredit.info.query.enums.CatalogEnum;
 import com.jkcredit.info.query.service.InformationQueryService;
 import com.jkcredit.info.query.task.AsyncQuery;
 import com.jkcredit.info.query.util.CatalogConcurrentHashMapUtil;
+import com.jkcredit.info.query.util.CookieTaskConcurrentHashMapUtil;
 import com.jkcredit.info.query.util.CookieUtil;
 import com.jkcredit.info.query.util.HttpUtil;
 import lombok.extern.slf4j.Slf4j;
@@ -22,7 +23,9 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import java.io.UnsupportedEncodingException;
-import java.net.*;
+import java.net.InetAddress;
+import java.net.URLEncoder;
+import java.net.UnknownHostException;
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.concurrent.CountDownLatch;
@@ -55,7 +58,7 @@ public class InformationQueryServiceImpl implements InformationQueryService {
                 + UUID.randomUUID().toString();
         log.info("keyWord:{}, type:{}, requestId:{}", keyWord, type, requestId);
         long startTime = System.currentTimeMillis();
-        String cookie = CookieUtil.getAuthenticationCookie();
+        String cookie = getCookie();
         if (StringUtils.isBlank(cookie)) {
             CommonResponseObject result = CommonResponseObject.failed("查询失败",
                     CommonConstant.NETWORK_DISABLED, getLocalHost(), requestId);
@@ -114,13 +117,40 @@ public class InformationQueryServiceImpl implements InformationQueryService {
         }
     }
 
+    private String getCookie() {
+        Map<String, Long> cookieTaskMap = CookieTaskConcurrentHashMapUtil.getInstance();
+        if (cookieTaskMap.size() == 0) {
+            String cookie = CookieUtil.getNewCookie();
+            if (StringUtils.isNotBlank(cookie)) {
+                log.info("firstGetCookie:{}", cookie);
+                CookieTaskConcurrentHashMapUtil.getInstance().put("cookie", System.currentTimeMillis());
+                log.info("InformationQueryServiceImpl.getCookie:{}", cookie);
+            }
+            return cookie;
+        } else {
+            long cookieTime = cookieTaskMap.get("cookie");
+            if (System.currentTimeMillis() - cookieTime >= 1620000) {
+                String cookie = CookieUtil.getNewCookie();
+                if (StringUtils.isNotBlank(cookie)) {
+                    log.info("reloadCookie:{}", cookie);
+                    CookieTaskConcurrentHashMapUtil.getInstance().put("cookie", System.currentTimeMillis());
+                    log.info("InformationQueryServiceImpl.getCookie:{}", cookie);
+                }
+                return cookie;
+            } else {
+                log.info("usefulCookie:{}", CookieUtil.getAuthenticationCookie());
+                return CookieUtil.getAuthenticationCookie();
+            }
+        }
+    }
+
     @Override
     public CommonResponseObject queryList(String keyWord, String type) {
         String requestId = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())
                 + UUID.randomUUID().toString();
         log.info("keyWord:{}, type:{}, requestId:{}", keyWord, type, requestId);
         long startTime = System.currentTimeMillis();
-        String cookie = CookieUtil.getAuthenticationCookie();
+        String cookie = getCookie();
         if (StringUtils.isBlank(cookie)) {
             CommonResponseObject result = CommonResponseObject.failed("查询失败",
                     CommonConstant.NETWORK_DISABLED, getLocalHost(), requestId);
@@ -184,6 +214,15 @@ public class InformationQueryServiceImpl implements InformationQueryService {
         long startTime = System.currentTimeMillis();
         String requestId = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())
                 + UUID.randomUUID().toString();
+        String cookie = getCookie();
+        if (StringUtils.isBlank(cookie)) {
+            CommonResponseObject result = CommonResponseObject.failed("查询失败",
+                    CommonConstant.NETWORK_DISABLED, getLocalHost(), requestId);
+            log.info("path:{}, requestId:{}, result:{}, costTime:{}"
+                    , path, requestId, JSON.toJSONString(result), (System.currentTimeMillis() - startTime));
+            return result;
+        }
+
         log.info("path:{}, requestId:{}", path, requestId);
         if (!path.contains("../")) {
             CommonResponseObject result = CommonResponseObject.noResult(getLocalHost(), requestId);
@@ -191,7 +230,7 @@ public class InformationQueryServiceImpl implements InformationQueryService {
                     , path, requestId, JSON.toJSONString(result), (System.currentTimeMillis() - startTime));
             return result;
         }
-        List<Map<String, String>> results = getDetail(path);
+        List<Map<String, String>> results = getDetail(path, cookie);
         if (results.size() != 0) {
             CommonResponseObject result = CommonResponseObject.success(results, getLocalHost(), requestId);
             log.info("path:{}, requestId:{}, result:{}, costTime:{}"
@@ -211,6 +250,16 @@ public class InformationQueryServiceImpl implements InformationQueryService {
         Document doc = Jsoup.parse(content);
         Elements elementList = doc.select("td").select(".column");
         for (Element element : elementList) {
+            String imageUrl = "";
+            String url = element.select(".detailImage").attr("data-url");
+            if (url.contains("/photoSearch/singlePhotoSearch.action")) {
+                imageUrl = url.replace("../photoSearch/singlePhotoSearch.action",
+                                "http://51.1.0.67:80/info-query/getImage");
+            } else if (url.contains("/crjPhotoSearch/singlePhotoSearch.action")) {
+                imageUrl = url.replace("../crjPhotoSearch/singlePhotoSearch.action",
+                                "http://51.1.0.67:80/info-query/getCrjImage");
+            }
+
             Elements tdList = element.select(".kyfuse-form-value");
             Map<String, String> tdMap = new HashMap<>();
             for (Element td : tdList) {
@@ -224,21 +273,34 @@ public class InformationQueryServiceImpl implements InformationQueryService {
             } else {
                 tdMap.put("detailPath", element.select("a").attr("url"));
             }
+            tdMap.put("imageUrl", imageUrl);
         }
         return results;
     }
 
-    private List<Map<String, String>> getDetail(String path) {
+    private List<Map<String, String>> getDetail(String path, String cookie) {
         List<Map<String, String>> results = new ArrayList<>();
         String url = path.replace("../", hostUrl).replace("amp;", "");
-        String detailInfoContent = HttpUtil.doGet(url, CookieUtil.getAuthenticationCookie());
+        String detailInfoContent = HttpUtil.doGet(url, cookie);
         Document detailDoc = Jsoup.parse(detailInfoContent);
         Elements elements = detailDoc.select(".detail-info-list");
+
+        String imageUrl = "";
+        String srcUrl = detailDoc.select(".detail-phone").attr("src");
+        if (srcUrl.contains("/photoSearch/singlePhotoSearch.action")) {
+            imageUrl = srcUrl.replace("/photoSearch/singlePhotoSearch.action",
+                    "http://51.1.0.67:80/info-query/getImage");
+        } else if (srcUrl.contains("/crjPhotoSearch/singlePhotoSearch.action")) {
+            imageUrl = srcUrl.replace("/crjPhotoSearch/singlePhotoSearch.action",
+                    "http://51.1.0.67:80/info-query/getCrjImage");
+        }
+
         Map<String, String> detailMap = new HashMap<>();
         for (int i = 0; i < elements.size(); i++) {
             String dataName = detailDoc.select(".detail-info-list").get(i).select("span").get(1).attr("data-name");
             String dataValue = detailDoc.select(".detail-info-list").get(i).select("span").get(1).text();
             detailMap.put(dataName, dataValue);
+            detailMap.put("imageUrl", imageUrl);
         }
         if (detailMap.size() != 0) {
             results.add(detailMap);

+ 1 - 1
src/main/java/com/jkcredit/info/query/task/LoadCookieTask.java

@@ -17,7 +17,7 @@ import java.util.Random;
  * @version V1.0
  **/
 @Slf4j
-@Component
+//@Component
 public class LoadCookieTask {
     @Scheduled(cron = "0 0/3 * * * ?", zone = "Asia/Shanghai")
     public void loadCookie() {