From 7126eab629d31beb5164b576a44b865a6a00f07c Mon Sep 17 00:00:00 2001
From: lyh <925863403@qq.com>
Date: 星期三, 17 九月 2025 17:56:29 +0800
Subject: [PATCH] 420工控网修改回传

---
 src/main/java/com/lxzn/framework/utils/file/FileUtil.java |   77 +++++++++++++++++++++++++-------------
 1 files changed, 51 insertions(+), 26 deletions(-)

diff --git a/src/main/java/com/lxzn/framework/utils/file/FileUtil.java b/src/main/java/com/lxzn/framework/utils/file/FileUtil.java
index 3d7beb6..704cf3c 100644
--- a/src/main/java/com/lxzn/framework/utils/file/FileUtil.java
+++ b/src/main/java/com/lxzn/framework/utils/file/FileUtil.java
@@ -23,6 +23,8 @@
 import java.nio.file.Paths;
 import java.nio.file.StandardCopyOption;
 import java.security.MessageDigest;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -241,35 +243,58 @@
         return result;
     }
 
-    public static boolean copyFileRec(String lastFile) {
-        String absolutePathSend =  lastFile;
-        //瀛楃涓叉浛鎹�
-        String absolutePathSendNotFile =  lastFile.replace("/REC/","/bak_rec/");
-        String fileNameNoPath = (new File(lastFile)).getName();
-        String suffix = getFileSuffix(fileNameNoPath);
-        Integer recNum = absolutePathSendNotFile.lastIndexOf(fileNameNoPath);
-        if (suffix == null) {
-            absolutePathSendNotFile = absolutePathSendNotFile.substring(0,recNum) + fileNameNoPath+ "-" +
-                    DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_TIME_FULL);
+    public static boolean copyFileRec(String sourceFilePath) {
+        // 楠岃瘉杈撳叆璺緞
+        if (sourceFilePath == null || sourceFilePath.isEmpty()) {
+            System.err.println("Invalid source file path");
+            return false;
+        }
+
+        Path sourcePath = Paths.get(sourceFilePath);
+
+        // 楠岃瘉婧愭枃浠跺瓨鍦�
+        if (!Files.exists(sourcePath)) {
+            System.err.println("Source file does not exist: " + sourceFilePath);
+            return false;
+        }
+
+        // 鏋勫缓鐩爣鐩綍璺緞锛堟浛鎹�/REC/涓�/bak_rec/锛�
+        String targetDirPath = sourcePath.getParent().toString()
+                .replace("/REC/", "/bak_rec/")
+                .replace("\\REC\\", "\\bak_rec\\");
+
+        // 纭繚鏇挎崲鎴愬姛 - 濡傛灉鏈浛鎹㈠垯鎵嬪姩鏋勫缓璺緞
+        if (targetDirPath.equals(sourcePath.getParent().toString())) {
+            targetDirPath = sourcePath.getParent().toString().replace("REC", "bak_rec");
+        }
+
+        Path targetDir = Paths.get(targetDirPath);
+
+        // 鐢熸垚甯︽椂闂存埑鐨勬柊鏂囦欢鍚�
+        String fileName = sourcePath.getFileName().toString();
+        String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
+        String newFileName;
+
+        int dotIndex = fileName.lastIndexOf('.');
+        if (dotIndex > 0) {
+            String baseName = fileName.substring(0, dotIndex);
+            String extension = fileName.substring(dotIndex + 1);
+            newFileName = baseName + "-" + timestamp + "." + extension;
         } else {
-            String name = fileNameNoPath.replace("." + suffix,"");
-            absolutePathSendNotFile = absolutePathSendNotFile.substring(0,recNum) + name+ "-" +
-                    DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_TIME_FULL) + "." + suffix;
+            newFileName = fileName + "-" + timestamp;
         }
-        try{
-            FileChannel in = new FileInputStream(absolutePathSend).getChannel();
-            File file = new File(absolutePathSendNotFile);
-            if (!file.getParentFile().exists()){
-                //鏂囦欢澶逛笉瀛樺湪 鐢熸垚
-                file.getParentFile().mkdirs();
-            }
-            FileChannel out = new FileOutputStream(absolutePathSendNotFile, true).getChannel();
-            out.transferFrom(in, 0, in.size());
-            in.close();
-            out.close();
+
+        Path targetPath = targetDir.resolve(newFileName);
+
+        try {
+            // 鍒涘缓鐩爣鐩綍锛堝寘鎷墍鏈変笉瀛樺湪鐨勭埗鐩綍锛�
+            Files.createDirectories(targetDir);
+
+            // 浣跨敤NIO澶嶅埗鏂囦欢
+            Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
             return true;
-        }
-        catch (IOException e) {
+        } catch (IOException e) {
+            System.err.println("File copy failed: " + e.getMessage());
             e.printStackTrace();
             return false;
         }

--
Gitblit v1.9.3