孟令鑫 1 jaar geleden
bovenliggende
commit
f11ce395fd

+ 17 - 0
src/main/java/com/cicv/oss/config/adapter/controller/RosController.java

@@ -2,6 +2,7 @@ package com.cicv.oss.config.adapter.controller;
 
 import com.cicv.oss.config.adapter.entity.OssConfigInfo;
 import com.cicv.oss.config.infra.config.OssConfig;
+import com.cicv.oss.config.infra.config.util.MyFileUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.ResourceLoader;
@@ -39,5 +40,21 @@ public class RosController {
         }
     }
 
+    @GetMapping("/configString")
+    public String configString(HttpServletResponse response) {
+        try {
+            response.setContentType("application/octet-stream;charset=utf-8");
+            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("config.yaml", "utf-8"));
+            byte[] buffer = new byte[1024];
+//            org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:config.yaml");
+//            InputStream inputStream = resource.getInputStream();
+            InputStream inputStream = Files.newInputStream(Paths.get("/mnt/disk001/oss-config/yaml/config.yaml"));
+            return MyFileUtil.read(inputStream);
+
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
 
 }

+ 51 - 0
src/main/java/com/cicv/oss/config/infra/config/util/MyFileUtil.java

@@ -0,0 +1,51 @@
+package com.cicv.oss.config.infra.config.util;
+
+import lombok.SneakyThrows;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+
+public class MyFileUtil {
+
+    public static String read(String path) {
+        return read(new File(path));
+    }
+
+    public static String read(File file) {
+        try {
+            return read(Files.newInputStream(file.toPath()), 4096);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static String read(InputStream inputStream) {
+        return read(inputStream, 4096);
+    }
+
+    public static String read(File file, int bufferLength) {
+        try {
+            return read(Files.newInputStream(file.toPath()), bufferLength);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static String read(InputStream inputStream, int bufferLength) {
+        try {
+            StringBuilder result = new StringBuilder();
+            byte[] buf = new byte[bufferLength];//创建字节数组,存储临时读取的数据
+            int len;//记录数据读取的长度
+            //循环读取数据
+            while ((len = inputStream.read(buf)) != -1) { //长度为-1则读取完毕
+                result.append(new String(buf, 0, len)).append("\n");
+            }
+            inputStream.close();
+            return result.toString();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+}