Эх сурвалжийг харах

设备同步 代码优化提交

jingyuanchao 1 жил өмнө
parent
commit
3b11f8c944

+ 2 - 2
soc-api/soc-api-system/src/main/java/com/xunmei/system/api/RemoteFileService.java

@@ -96,6 +96,6 @@ public interface RemoteFileService {
     @GetMapping(value = "/file/deletedZipFile")
     void deletedZipFile();
 
-    @PostMapping(value = "/file/readFile/{path}", consumes = MediaType.APPLICATION_JSON_VALUE)
-    Response readFile(@PathVariable String path);
+    @PostMapping(value = "/file/readFile")
+    Response readFile(@RequestBody Map<String, String> data);
 }

+ 1 - 1
soc-api/soc-api-system/src/main/java/com/xunmei/system/api/factory/RemoteFileFallbackFactory.java

@@ -93,7 +93,7 @@ public class RemoteFileFallbackFactory implements FallbackFactory<RemoteFileServ
             }
 
             @Override
-            public Response readFile(String path) {
+            public Response readFile(Map<String, String> data) {
                 throw new RuntimeException("读取文件超时,请稍后重试!");
             }
         };

+ 13 - 9
soc-modules/soc-modules-file/src/main/java/com/xunmei/file/controller/SysFileController.java

@@ -59,8 +59,8 @@ public class SysFileController {
     @PostMapping("/upload")
     public R<SysFile> upload(MultipartFile file) {
         try {
-            if(Objects.requireNonNull(file.getOriginalFilename()).length() > 54){
-               return R.fail("上传文件名长度不能大于50!");
+            if (Objects.requireNonNull(file.getOriginalFilename()).length() > 54) {
+                return R.fail("上传文件名长度不能大于50!");
             }
 
             // 上传并返回访问地址
@@ -82,7 +82,7 @@ public class SysFileController {
     @PostMapping("/uploadFile")
     public R<SysFile> uploadFile(MultipartFile file, String busType) {
         try {
-            if(Objects.requireNonNull(file.getOriginalFilename()).length() > 54){
+            if (Objects.requireNonNull(file.getOriginalFilename()).length() > 54) {
                 return R.fail("上传文件名长度不能大于50!");
             }
             // 上传并返回访问地址
@@ -150,24 +150,28 @@ public class SysFileController {
         return R.ok(sysFileService.generateEduTrainingPdf(data));
 
     }
+
     @ApiOperation(value = "生成履职pdf")
     @PostMapping(value = "/generateResumptionPdf")
     R<String> generateResumptionPdf(@RequestBody Map<String, Object> data) throws Exception {
         return R.ok(sysFileService.generateResumptionPdf(data));
 
     }
+
     @ApiOperation(value = "生成安全检查pdf")
     @PostMapping(value = "/generateSafeCheckPdf")
     R<String> generateSafeCheckPdf(@RequestBody SafeCheckTaskRegisterBookVo data) throws Exception {
         return R.ok(sysFileService.generateSafeCheckPdf(data));
 
     }
+
     @ApiOperation(value = "生成监控调阅pdf")
     @PostMapping(value = "/generateAccessPdf")
     R<String> generateAccessPdf(@RequestBody AccessPdf data) throws Exception {
         return R.ok(sysFileService.generateAccessPdf(data));
 
     }
+
     @ApiOperation(value = "预案演练pdf")
     @PostMapping(value = "/generateDrillPdf")
     R<String> generateDrillPdf(@RequestBody Map<String, Object> data) throws Exception {
@@ -202,21 +206,21 @@ public class SysFileController {
 
     @PostMapping(value = "/registerBookCompressPdf")
     void registerBookCompressPdf(@RequestBody ExportPdfDto exportDto, HttpServletResponse response) throws IOException {
-        sysFileService.registerBookCompressPdf(exportDto,response);
+        sysFileService.registerBookCompressPdf(exportDto, response);
     }
 
     @PostMapping(value = "/cutFileCompress")
-    void cutFileCompress(@RequestBody CoreRegisterBookPdfExportDto exportDto){
+    void cutFileCompress(@RequestBody CoreRegisterBookPdfExportDto exportDto) {
         sysFileService.cutFileCompress(exportDto);
     }
 
     @GetMapping(value = "/deletedZipFile")
-    void deletedZipFile(){
+    void deletedZipFile() {
         sysFileService.deletedZipFile();
     }
 
-    @GetMapping(value = "/readFile/{path}")
-    void readFile(@PathVariable String path,HttpServletResponse response)throws IOException {
-        sysFileService.readFile(path,response);
+    @PostMapping(value = "/readFile")
+    void readFile(@RequestBody Map<String, String> data, HttpServletResponse response) throws IOException {
+        sysFileService.readFile(data.get("path"), response);
     }
 }

+ 10 - 4
soc-modules/soc-modules-file/src/main/java/com/xunmei/file/service/LocalSysFileServiceImpl.java

@@ -52,6 +52,8 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
 import java.math.BigDecimal;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
@@ -939,18 +941,22 @@ public class LocalSysFileServiceImpl implements ISysFileService {
     }
 
     @Override
-    public void readFile(String path,HttpServletResponse response) throws IOException {
+    public void readFile(String path, HttpServletResponse response) throws IOException {
         if (ObjectUtil.isEmpty(path)) {
             throw new RuntimeException("文件地址无效");
         }
         // 对网络地址进行解码
         path = URLDecoder.decode(path, "UTF-8");
         // 读取图片文件
-        File imageFile = new File(path);
-        try (BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(imageFile.toPath()))) {
+        URL url = new URL(path);
+        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+        conn.setRequestMethod("GET");
+        conn.setConnectTimeout(5 * 1000);
+        InputStream stream = conn.getInputStream();
+        try (BufferedInputStream bis = new BufferedInputStream(stream)) {
             // 设置响应类型
             response.setContentType("image/*");
-            response.setHeader("Content-Length", String.valueOf(imageFile.length()));
+            response.setHeader("Content-Length", String.valueOf(stream.available()));
             // 将文件流发送给前端
             byte[] buffer = new byte[1024];
             int bytesRead;

+ 7 - 8
soc-modules/soc-modules-mediator/src/main/java/com/xunmei/mediator/api/controller/DataController.java

@@ -17,10 +17,7 @@ import com.xunmei.system.api.dto.protection.ReceiveErrorDto;
 import com.xunmei.system.api.util.LogUtils;
 import feign.Response;
 import io.swagger.annotations.ApiOperation;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
@@ -28,7 +25,9 @@ import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @author jingyuanchao
@@ -180,13 +179,13 @@ public class DataController {
     private RemoteFileService fileService;
 
     @ApiOperation("传感器通用设备信息同步")
-    @PostMapping({"/api/test/{path}"})
-    public void sensor(@PathVariable String path, HttpServletResponse response) throws IOException {
-        Response result = fileService.readFile(path);
+    @GetMapping({"/api/test"})
+    public void test(@RequestBody Map<String, String> data, HttpServletResponse response) throws IOException {
+        Response result = fileService.readFile(data);
         Response.Body body = result.body();
         InputStream inputStream = body.asInputStream();
+        System.out.println(inputStream.available());
         BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
-        response.setHeader("Content-Disposition", result.headers().get("Content-Disposition").toString().replace("[", "").replace("]", ""));
         BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
         int length = 0;
         byte[] temp = new byte[1024 * 10];