소스 검색

修复bug

hjp 3 달 전
부모
커밋
0c03f2f5cd
1개의 변경된 파일11개의 추가작업 그리고 2개의 파일을 삭제
  1. 11 2
      src/main/java/com/jg/service/impl/ModBusServiceImpl.java

+ 11 - 2
src/main/java/com/jg/service/impl/ModBusServiceImpl.java

@@ -21,7 +21,9 @@ import org.springframework.context.event.EventListener;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.math.BigDecimal;
 import java.net.URISyntaxException;
 import java.nio.charset.StandardCharsets;
@@ -71,8 +73,15 @@ public class ModBusServiceImpl implements ModBusService {
 //        log.warn("[开始连接采集模块]: {}", JSON.toJSONString(collectModuleInfoList));
         ModbusMaster master = ModBusUtil.createMaster(host, port);
         MASTER_MAP.put(1l, master);
-        try {
-            show = new String(Files.readAllBytes(Paths.get(ClassLoader.getSystemResource("tag.json").toURI())), StandardCharsets.UTF_8);
+        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("tag.json");
+        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()){
+                byte[] buffer = new byte[1024];
+                int length;
+                while ((length = inputStream.read(buffer)) != -1) {
+                    byteArrayOutputStream.write(buffer, 0, length);
+                }
+                show = byteArrayOutputStream.toString(StandardCharsets.UTF_8.name());
+                // 使用 show 字符串
         } catch (Exception e) {
             e.printStackTrace();
         }