Преглед на файлове

添加获取不到设备信息时返回值

zhaojinyu преди 11 часа
родител
ревизия
154c918ddc
променени са 1 файла, в които са добавени 13 реда и са изтрити 6 реда
  1. 13 6
      base-modules/service-license/service-license-common/src/main/java/com/usky/license/common/OshiServerInfos.java

+ 13 - 6
base-modules/service-license/service-license-common/src/main/java/com/usky/license/common/OshiServerInfos.java

@@ -34,11 +34,15 @@ public class OshiServerInfos {
 
 
     /* =============== IP 地址 =============== */
     /* =============== IP 地址 =============== */
     private List<String> getIpAddress() throws Exception {
     private List<String> getIpAddress() throws Exception {
-        return getLocalAllInetAddress().stream()
+        List<String> ipAddresses = getLocalAllInetAddress().stream()
                 .map(InetAddress::getHostAddress)
                 .map(InetAddress::getHostAddress)
                 .distinct()
                 .distinct()
                 .map(String::toLowerCase)
                 .map(String::toLowerCase)
                 .collect(Collectors.toList());
                 .collect(Collectors.toList());
+        if (ipAddresses.isEmpty()) {
+            ipAddresses.add("获取IP地址失败");
+        }
+        return ipAddresses;
     }
     }
 
 
     /* =============== MAC 地址 =============== */
     /* =============== MAC 地址 =============== */
@@ -53,13 +57,13 @@ public class OshiServerInfos {
     /* =============== CPU 序列号 =============== */
     /* =============== CPU 序列号 =============== */
     private String getCPUSerial() throws Exception {
     private String getCPUSerial() throws Exception {
         String serial = SI.getHardware().getProcessor().getProcessorID().trim();
         String serial = SI.getHardware().getProcessor().getProcessorID().trim();
-        return serial.isEmpty() ? null : serial;
+        return serial.isEmpty() || "Default string".equals(serial) ? "获取CPU序列号失败" : serial;
     }
     }
 
 
     /* =============== 主板序列号 =============== */
     /* =============== 主板序列号 =============== */
     private String getMainBoardSerial() throws Exception {
     private String getMainBoardSerial() throws Exception {
         String serial = SI.getHardware().getComputerSystem().getSerialNumber().trim();
         String serial = SI.getHardware().getComputerSystem().getSerialNumber().trim();
-        return serial.isEmpty() ? null : serial;
+        return serial.isEmpty() || "Default string".equals(serial) ? "获取主板序列号失败" : serial;
     }
     }
 
 
     /* =============== 网络接口工具 =============== */
     /* =============== 网络接口工具 =============== */
@@ -83,7 +87,10 @@ public class OshiServerInfos {
     private String getMacByInetAddress(InetAddress inet) {
     private String getMacByInetAddress(InetAddress inet) {
         try {
         try {
             byte[] mac = NetworkInterface.getByInetAddress(inet).getHardwareAddress();
             byte[] mac = NetworkInterface.getByInetAddress(inet).getHardwareAddress();
-            if (mac == null) return null;
+            if (mac == null) {
+                log.warn("无法获取MAC地址: {}", inet.getHostAddress());
+                return "获取MAC地址失败";
+            }
             StringBuilder sb = new StringBuilder();
             StringBuilder sb = new StringBuilder();
             for (int i = 0; i < mac.length; i++) {
             for (int i = 0; i < mac.length; i++) {
                 if (i != 0) sb.append("-");
                 if (i != 0) sb.append("-");
@@ -93,8 +100,8 @@ public class OshiServerInfos {
             }
             }
             return sb.toString().toUpperCase();
             return sb.toString().toUpperCase();
         } catch (SocketException e) {
         } catch (SocketException e) {
-            log.error("获取MAC失败", e);
-            return null;
+            log.error("获取MAC地址失败: {}", inet.getHostAddress(), e);
+            return "获取MAC地址失败";
         }
         }
     }
     }
 }
 }