Browse Source

提交保存,修复照片无法显示问题

xusonglin 2 years ago
parent
commit
ff24737249

+ 1 - 1
pom.xml

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

+ 22 - 2
src/main/java/com/jkcredit/info/query/controller/InfoQueryController.java

@@ -3,7 +3,11 @@ package com.jkcredit.info.query.controller;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.JSONObject;
 import com.jkcredit.common.model.CommonResponseObject;
 import com.jkcredit.common.model.CommonResponseObject;
 import com.jkcredit.info.query.service.InformationQueryService;
 import com.jkcredit.info.query.service.InformationQueryService;
+import com.jkcredit.info.query.util.SSLSocketClient;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Response;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.cloud.client.discovery.DiscoveryClient;
 import org.springframework.cloud.client.discovery.DiscoveryClient;
@@ -16,8 +20,11 @@ import org.springframework.web.bind.annotation.RestController;
 
 
 import javax.imageio.ImageIO;
 import javax.imageio.ImageIO;
 import java.awt.image.BufferedImage;
 import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
 import java.net.URL;
+import java.util.concurrent.TimeUnit;
 
 
 @RestController
 @RestController
 @Slf4j
 @Slf4j
@@ -55,11 +62,19 @@ public class InfoQueryController {
         urlStringBuffer.append("&gmsfhm=").append(gmsfhm);
         urlStringBuffer.append("&gmsfhm=").append(gmsfhm);
         String imageUrl = urlStringBuffer.toString();
         String imageUrl = urlStringBuffer.toString();
         log.info("getImage.imageUrl:{}", imageUrl);
         log.info("getImage.imageUrl:{}", imageUrl);
+
+        InputStream imageStream = informationQueryService.getImageStream(imageUrl);
+        if (imageStream == null) {
+            log.info("imageStream == null");
+            return null;
+        }
         try {
         try {
-            return ImageIO.read(new URL(imageUrl));
+            return ImageIO.read(imageStream);
         } catch (Exception e) {
         } catch (Exception e) {
             log.error("getImage.url:{},Exception:{}", urlStringBuffer.toString(), e);
             log.error("getImage.url:{},Exception:{}", urlStringBuffer.toString(), e);
         }
         }
+        log.info("imageStream != null");
+
         return null;
         return null;
     }
     }
 
 
@@ -73,8 +88,13 @@ public class InfoQueryController {
         urlStringBuffer.append("&jgmc=").append(jgmc);
         urlStringBuffer.append("&jgmc=").append(jgmc);
         String imageUrl = urlStringBuffer.toString();
         String imageUrl = urlStringBuffer.toString();
         log.info("getCrjImage.imageUrl:{}", imageUrl);
         log.info("getCrjImage.imageUrl:{}", imageUrl);
+
+        InputStream imageStream = informationQueryService.getImageStream(imageUrl);
+        if (imageStream == null) {
+            return null;
+        }
         try {
         try {
-            return ImageIO.read(new URL(imageUrl));
+            return ImageIO.read(imageStream);
         } catch (Exception e) {
         } catch (Exception e) {
             log.error("getImage.url:{},Exception:{}", urlStringBuffer.toString(), e);
             log.error("getImage.url:{},Exception:{}", urlStringBuffer.toString(), e);
         }
         }

+ 4 - 0
src/main/java/com/jkcredit/info/query/service/InformationQueryService.java

@@ -3,6 +3,8 @@ package com.jkcredit.info.query.service;
 
 
 import com.jkcredit.common.model.CommonResponseObject;
 import com.jkcredit.common.model.CommonResponseObject;
 
 
+import java.io.InputStream;
+
 /**
 /**
  * @author xusonglin
  * @author xusonglin
  * @version V1.0
  * @version V1.0
@@ -17,4 +19,6 @@ public interface InformationQueryService {
     CommonResponseObject queryList(String keyWord, String type);
     CommonResponseObject queryList(String keyWord, String type);
 
 
     CommonResponseObject queryDetail(String path);
     CommonResponseObject queryDetail(String path);
+
+    InputStream getImageStream(String url);
 }
 }

+ 12 - 0
src/main/java/com/jkcredit/info/query/service/impl/InformationQueryServiceImpl.java

@@ -22,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
+import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.io.UnsupportedEncodingException;
 import java.net.InetAddress;
 import java.net.InetAddress;
 import java.net.URLEncoder;
 import java.net.URLEncoder;
@@ -422,6 +423,17 @@ public class InformationQueryServiceImpl implements InformationQueryService {
         }
         }
     }
     }
 
 
+    @Override
+    public InputStream getImageStream(String url) {
+        String cookie = getCookie();
+        if (StringUtils.isBlank(cookie)) {
+            log.info("loadCookieFailed,url:{}", url);
+            return null;
+        }
+
+        return HttpUtil.doGetStream(url, cookie);
+    }
+
     private String getLocalHost() {
     private String getLocalHost() {
         InetAddress address = null;
         InetAddress address = null;
         try {
         try {

+ 30 - 0
src/main/java/com/jkcredit/info/query/util/HttpUtil.java

@@ -4,7 +4,9 @@ import com.jkcredit.info.query.constant.CommonConstant;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import okhttp3.*;
 import okhttp3.*;
 
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentHashMap;
@@ -112,6 +114,34 @@ public class HttpUtil {
         return responseContext;
         return responseContext;
     }
     }
 
 
+    public static InputStream doGetStream(String url, String cookie) {
+        Request okRequest = new Request.Builder().url(url)
+                .header("Cookie", cookie)
+                .build();
+
+        OkHttpClient client = okHttpClient.newBuilder()
+                .connectTimeout(3000, TimeUnit.MILLISECONDS)
+                .readTimeout(6000, TimeUnit.MILLISECONDS)
+                .writeTimeout(6000, TimeUnit.MILLISECONDS)
+                .sslSocketFactory(SSLSocketClient.getSocketFactory(), SSLSocketClient.getX509TrustManager())
+                .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
+                .build();
+        InputStream responseStream = null;
+
+        try {
+            Response response = client.newCall(okRequest).execute();
+            if (response.body() != null) {
+                responseStream =  new ByteArrayInputStream(response.body().bytes());
+            }
+            response.close();
+        } catch (IOException ioe) {
+            log.error("doGetStream.IOException:", ioe);
+        } catch (Exception e) {
+            log.error("doGetStream.Exception:", e);
+        }
+        return responseStream;
+    }
+
     public static void main(String[] args) {
     public static void main(String[] args) {
         System.setProperty("https.proxyHost", "127.0.0.1");
         System.setProperty("https.proxyHost", "127.0.0.1");
         System.setProperty("https.proxyPort", "9986");
         System.setProperty("https.proxyPort", "9986");