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

调整 同步机构 人员 mock接口写法,源写法打包jar后 无法读取文件

zhulu 8 сар өмнө
parent
commit
97d0193295

+ 67 - 28
soc-modules/soc-modules-sync/src/main/java/com/xunmei/sync/controller/FjnxSyncController.java

@@ -18,8 +18,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.io.FileInputStream;
-import java.io.IOException;
+import java.io.*;
 
 /**
  *
@@ -154,25 +153,44 @@ class FjnxSyncController {
     @GetMapping("/mock/orgxmldata")
     public void orgxmldata(HttpServletResponse response, HttpServletRequest request) {
         try {
-            FileInputStream in = null;
+//            FileInputStream in = null;
             response.setCharacterEncoding("UTF-8");
             response.setContentType("application/octet-stream;charset=utf-8");
             response.setHeader("Content-disposition", "attachment;filename=file.xml");
 
-            try {
-                String path = this.getClass().getClassLoader().getResource("fjnxorg.xml").getPath();
-                in = new FileInputStream(path);
-                byte[] a = new byte[1024];
-                int b;
-                while ((b = in.read(a)) != -1) {
-                    response.getOutputStream().write(a, 0, b);
+//            try {
+//                String path = this.getClass().getClassLoader().getResource("fjnxorg.xml").getPath();
+//                in = new FileInputStream(path);
+//                byte[] a = new byte[1024];
+//                int b;
+//                while ((b = in.read(a)) != -1) {
+//                    response.getOutputStream().write(a, 0, b);
+//                }
+//            } catch (Exception e) {
+//                e.printStackTrace();
+//            }
+            InputStream a =null;
+            try (InputStream in = this.getClass().getClassLoader().getResourceAsStream("fjnxorg.xml")) {
+                if (in == null) {
+                    throw new FileNotFoundException("Resource not found: fjnxorg.xml");
                 }
-            } catch (Exception e) {
-                e.printStackTrace();
-            } finally {
-                if (null != in) {
+                a=in;
+
+                byte[] buffer = new byte[1024];
+                int bytesRead;
+                OutputStream out = response.getOutputStream();
+                while ((bytesRead = in.read(buffer)) != -1) {
+                    out.write(buffer, 0, bytesRead);
+                }
+                out.flush(); // 确保所有数据都被写出
+            } catch (IOException e) {
+                log.error("Error reading or writing stream", e);
+                throw new RuntimeException("Error during file processing", e);
+            }
+            finally {
+                if (null != a) {
                     try {
-                        in.close();
+                        a.close();
                     } catch (IOException e2) {
                         System.out.println("关闭输入流错误");
                     }
@@ -194,25 +212,46 @@ class FjnxSyncController {
     @GetMapping("/mock/userxmldata")
     public void userxmldata(HttpServletResponse response, HttpServletRequest request) {
         try {
-            FileInputStream in = null;
+//            FileInputStream in = null;
             response.setCharacterEncoding("UTF-8");
             response.setContentType("application/octet-stream;charset=utf-8");
             response.setHeader("Content-disposition", "attachment;filename=file.xml");
 
-            try {
-                String path = this.getClass().getClassLoader().getResource("sync_fjnx_user.xml").getPath();
-                in = new FileInputStream(path);
-                byte[] a = new byte[1024];
-                int b;
-                while ((b = in.read(a)) != -1) {
-                    response.getOutputStream().write(a, 0, b);
+//            try {
+//                String path = this.getClass().getClassLoader().getResource("sync_fjnx_user.xml").getPath();
+//                in = new FileInputStream(path);
+//                byte[] a = new byte[1024];
+//                int b;
+//                OutputStream out = response.getOutputStream();
+//                while ((b = in.read(a)) != -1) {
+//                    out.write(a, 0, b);
+//                }
+//                out.flush();
+//            } catch (Exception e) {
+//                e.printStackTrace();
+//            }
+            InputStream a =null;
+            try (InputStream in = this.getClass().getClassLoader().getResourceAsStream("sync_fjnx_user.xml")) {
+                if (in == null) {
+                    throw new FileNotFoundException("Resource not found: sync_fjnx_user.xml");
                 }
-            } catch (Exception e) {
-                e.printStackTrace();
-            } finally {
-                if (null != in) {
+                a=in;
+
+                byte[] buffer = new byte[1024];
+                int bytesRead;
+                OutputStream out = response.getOutputStream();
+                while ((bytesRead = in.read(buffer)) != -1) {
+                    out.write(buffer, 0, bytesRead);
+                }
+                out.flush(); // 确保所有数据都被写出
+            } catch (IOException e) {
+                log.error("Error reading or writing stream", e);
+                throw new RuntimeException("Error during file processing", e);
+            }
+            finally {
+                if (null != a) {
                     try {
-                        in.close();
+                        a.close();
                     } catch (IOException e2) {
                         System.out.println("关闭输入流错误");
                     }