From 661570d3cef48b7df121f649fe00743770a9f16d Mon Sep 17 00:00:00 2001
From: zhanzhiqin <895896009@qq.com>
Date: 星期三, 18 五月 2022 13:43:41 +0800
Subject: [PATCH] fix

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/rrpc/util/FileProcessingUtils.java |  111 +++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 71 insertions(+), 40 deletions(-)

diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/rrpc/util/FileProcessingUtils.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/rrpc/util/FileProcessingUtils.java
index e5bbb77..3b75f38 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/rrpc/util/FileProcessingUtils.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/manager/iot/rrpc/util/FileProcessingUtils.java
@@ -2,12 +2,14 @@
 
 import com.sandu.ximon.admin.dto.RemoteFileDto;
 import com.sandu.ximon.admin.utils.HexUtils;
+import com.sandu.ximon.admin.utils.StringUtil;
 import org.apache.catalina.LifecycleState;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -17,45 +19,74 @@
  * @date 2022/5/5 14:26
  */
 public class FileProcessingUtils {
+    private static final int readSize = 512;
 
-//    public static void main(String[] args) {
-//        try {
-//            RemoteFileDto remoteFileDto = read("C:\\Users\\Administrator\\Desktop\\test\\H01_v3.0.12.min.dhcp.bin");
-//            System.out.println(remoteFileDto.getListSize());
-//            System.out.println(remoteFileDto.getList().size());
-//            System.out.println(Arrays.toString(remoteFileDto.getList().get(0)));
-//        } catch (IOException e) {
-//            e.printStackTrace();
-//        }
-//    }
-
-    public static RemoteFileDto read(String filePath) throws IOException {
-        List<String> data = new ArrayList<>();
-        int dataSize = 0;
-        StringBuffer fileHexStr=new StringBuffer();
-
-        File file = new File(filePath);
-
-        InputStream in = new FileInputStream(file);
-
-        // 涓�娆℃�у彇澶氬皯涓瓧鑺�
-        byte[] bytes = new byte[512];
-        // 璇诲彇鍒扮殑瀛楄妭鏁扮粍闀垮害锛屼负-1鏃惰〃绀烘病鏈夋暟鎹�
-        int length = 0;
-        // 寰幆鍙栨暟鎹�
-        while ((length = in.read(bytes)) != -1) {
-            String temp=HexUtils.encodeHexStr(Arrays.copyOf(bytes, length));
-            data.add(temp);
-            fileHexStr.append(temp);
-            dataSize += length;
+    public static RemoteFileDto read(String filePath) {
+        //鏂囦欢鍦板潃涓虹┖
+        if (filePath == null) {
+            return null;
         }
+        List<String> data = new ArrayList<>();
+        // 鍙栨暟鎹暱搴�
+        int dataSize = 0;
+        StringBuffer fileHexStr = new StringBuffer();
+        try {
+            // 鍒涘缓URL
+            URL url = new URL(filePath);
+            // 璇曞浘杩炴帴骞跺彇寰楄繑鍥炵姸鎬佺爜
+            URLConnection urlconn = url.openConnection();
+            urlconn.connect();
+            HttpURLConnection httpconn = (HttpURLConnection) urlconn;
+            int HttpResult = httpconn.getResponseCode();
+            if (HttpResult != HttpURLConnection.HTTP_OK) {
+                System.out.print("鏃犳硶杩炴帴鍒�");
+                return null;
+            } else {
+                InputStream in = urlconn.getInputStream();
 
-        in.close();
+                // 涓�娆℃�у彇澶氬皯涓瓧鑺�
+                byte[] bytes = new byte[readSize];
+                // 璇诲彇鍒扮殑瀛楄妭鏁扮粍闀垮害锛屼负-1鏃惰〃绀烘病鏈夋暟鎹�
+                int length = 0;
+                // 寰幆鍙栨暟鎹�
+                while ((length = in.read(bytes)) != -1) {
+                    String temp = HexUtils.encodeHexStr(Arrays.copyOf(bytes, length));
+                    if (!StringUtil.strIsNullOrEmpty(temp)) {
+                        fileHexStr.append(temp);
+                        dataSize += length;
+                    }
+                }
 
-        RemoteFileDto remoteFileDto = new RemoteFileDto();
-        remoteFileDto.setList(data);
-        remoteFileDto.setListSize(dataSize);
-        remoteFileDto.setFileHexStr(fileHexStr.toString());
-        return remoteFileDto;
+                in.close();
+                //鎸夋瘡甯ф暟鎹ぇ灏忚繘琛屽垏鍓叉埅鍙栨暟鎹�
+                String tempHexStr = fileHexStr.toString();
+                int c = readSize * 2;
+                int cl = -1;
+                while (!StringUtil.strIsNullOrEmpty(tempHexStr)) {
+                    if (tempHexStr.length() > c) {
+                        cl = c;
+                    } else {
+                        cl = tempHexStr.length();
+                    }
+                    data.add(tempHexStr.substring(0, cl));
+                    tempHexStr = tempHexStr.substring(cl, tempHexStr.length());
+                }
+
+                RemoteFileDto remoteFileDto = new RemoteFileDto();
+                remoteFileDto.setList(data);
+                remoteFileDto.setListSize(dataSize);
+                remoteFileDto.setFileHexStr(fileHexStr.toString());
+                return remoteFileDto;
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    public static void main(String[] args) {
+        RemoteFileDto t = read("https://ximonsmart.oss-cn-shanghai.aliyuncs.com/20220507175550f1c437-e2f6-413f-9a78-c1cfea3d2a82.bin");
+        System.out.println(t.getListSize());
+        System.out.println(t.getList().size());
     }
 }

--
Gitblit v1.9.3