Kaynağa Gözat

【CHG】导出二维码增加计时日志

ZhaoMn 3 yıl önce
ebeveyn
işleme
d0c1a1a5f4

+ 6 - 12
railway-business/src/main/java/com/railway/business/safetool/service/impl/BaseSafetyToolServiceImpl.java

@@ -228,21 +228,15 @@ public class BaseSafetyToolServiceImpl implements IBaseSafetyToolService {
     }
     for (BaseSafetyTool tool : tools){
       String jpgPath = tmpPath + tool.getToolCode() + ".jpg";
+      File file = new File(jpgPath);
       log.debug(" ++ {}", jpgPath);
-      try {
-        HttpUtils.download(tool.getQcodeUrl(), jpgPath);
-      } catch (Exception e) {
+      byte[] byteArray = HttpUtils.sendGetStream(tool.getQcodeUrl());
+      log.debug(" -- {}", tool.getQcodeUrl());
+      try (FileOutputStream os = new FileOutputStream(file, true)){
+        os.write(byteArray, 0, byteArray.length);
+      } catch (IOException e) {
         e.printStackTrace();
       }
-//      File file = new File(jpgPath);
-//
-//      byte[] byteArray = HttpUtils.sendGetStream(tool.getQcodeUrl());
-//      log.debug(" -- {}", tool.getQcodeUrl());
-//      try (FileOutputStream os = new FileOutputStream(file, true)){
-//        os.write(byteArray, 0, byteArray.length);
-//      } catch (IOException e) {
-//        e.printStackTrace();
-//      }
     }
     String zipFile;
     try {

+ 22 - 9
railway-common/src/main/java/com/railway/common/utils/http/HttpUtils.java

@@ -112,13 +112,7 @@ public class HttpUtils {
     }
     log.info("sendGet - {}", urlNameString);
     try {
-      urlNameString = getRedirectUrl(urlNameString);
-      URL realUrl = new URL(urlNameString);
-      URLConnection connection = realUrl.openConnection();
-      connection.setRequestProperty("accept", "*/*");
-      connection.setRequestProperty("connection", "Keep-Alive");
-      connection.setRequestProperty("user-agent",
-          "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
+      URLConnection connection = getRedirectUrlConnection(urlNameString);
       connection.connect();
       return connection.getInputStream();
     } catch (ConnectException e) {
@@ -260,8 +254,7 @@ public class HttpUtils {
    * @throws Exception 读写异常
    */
   private static String getRedirectUrl(String path) throws Exception {
-    HttpURLConnection conn = (HttpURLConnection) new URL(path)
-        .openConnection();
+    HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
     conn.setInstanceFollowRedirects(false);
     conn.setConnectTimeout(5000);
     String redirectUrl = conn.getHeaderField("Location");
@@ -271,6 +264,26 @@ public class HttpUtils {
     return redirectUrl;
   }
 
+  /**
+   * 获取重定向地址
+   * @param path 原URL地址
+   * @return 重定向之后的地址
+   * @throws Exception 读写异常
+   */
+  private static URLConnection getRedirectUrlConnection(String path) throws Exception {
+    HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
+    conn.setInstanceFollowRedirects(false);
+    conn.setConnectTimeout(5000);
+    String redirectUrl = conn.getHeaderField("Location");
+    if(null != redirectUrl){
+      URL realUrl = new URL(redirectUrl);
+      conn = (HttpURLConnection) realUrl.openConnection();
+    }
+    conn.setRequestProperty("accept", "*/*");
+    conn.setRequestProperty("connection", "Keep-Alive");
+    conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
+    return conn;
+  }
 
   /**
    * 将输入流中的数据写入字节数组