浏览代码

质检-同步合同段-本地文件不存在,就通过oss获取文件

LHB 1 月之前
父节点
当前提交
82c70204d9

+ 10 - 2
blade-service/blade-business/src/main/java/org/springblade/business/controller/InformationWriteQueryController.java

@@ -80,6 +80,8 @@ import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
 import java.io.*;
 import java.net.URLEncoder;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
@@ -1053,8 +1055,14 @@ public class InformationWriteQueryController extends BladeController {
                             oldHtml.setId(SnowFlakeUtil.getId());
                             oldHtml.setCreateUser(getUser().getUserId());
                             String htmlUrl = wbsTreeContract.getHtmlUrl();
-                            String s = FileUtils.copyFileWithAbsolutePath(htmlUrl);
-                            oldHtml.setOldHtmlUrl(FileUtils.copyFileWithAbsolutePath(htmlUrl));
+                            // 获取或下载文件
+                            Path sourcePath = FileUtils.getOrDownloadFile(htmlUrl);
+                            // 生成副本路径
+                            Path copyPath = FileUtils.generateCopyPath(sourcePath);
+                            // 复制文件
+                            Files.copy(sourcePath, copyPath);
+
+                            oldHtml.setOldHtmlUrl(copyPath.toFile().getAbsolutePath());
                             data.add(oldHtml);
                         }
                         wbsTreeContractOldHtmlClient.save(data);

+ 48 - 0
blade-service/blade-business/src/main/java/org/springblade/business/utils/FileUtils.java

@@ -31,6 +31,7 @@ import java.awt.geom.AffineTransform;
 import java.awt.image.AffineTransformOp;
 import java.awt.image.BufferedImage;
 import java.io.*;
+import java.net.URL;
 import java.net.URLEncoder;
 import java.nio.file.*;
 import java.util.Arrays;
@@ -377,6 +378,11 @@ public class FileUtils {
         try {
             // 1. 获取源文件的绝对路径
             File sourceFile = new File(sourcePath);
+            if(!sourceFile.exists()){
+                String path = getNetUrl(sourcePath);
+                InputStream ossInputStream = CommonUtil.getOSSInputStream(getNetUrl(sourcePath));
+            }
+
             String absolutePath = sourceFile.getAbsolutePath();
             System.out.println("源文件绝对路径: " + absolutePath);
 
@@ -445,4 +451,46 @@ public class FileUtils {
             throw new ServiceException("IO错误");
         }
     }
+
+    /**
+     * 获取本地文件,若不存在则从URL下载
+     */
+    public static Path getOrDownloadFile(String localPath) throws IOException {
+        Path path = Paths.get(localPath);
+
+        // 如果本地文件不存在,则从URL下载
+        if (!Files.exists(path)) {
+            System.out.println("本地文件不存在,开始下载...");
+            // 确保父目录存在
+            Path parentDir = path.getParent();
+            if (parentDir != null && !Files.exists(parentDir)) {
+                Files.createDirectories(parentDir);
+            }
+
+            // 从URL下载文件
+            try (InputStream in = CommonUtil.getOSSInputStream(getNetUrl(localPath))) {
+                Files.copy(in, path);
+                System.out.println("文件下载成功: " + path);
+            } catch (IOException e) {
+                throw new IOException("下载文件失败: " + e.getMessage(), e);
+            }
+        }
+        return path;
+    }
+
+    /**
+     * 生成带后缀的副本路径
+     */
+    public static Path generateCopyPath(Path originalPath) {
+        String suffix = "_copy";
+        String fileName = originalPath.getFileName().toString();
+        int dotIndex = fileName.lastIndexOf('.');
+
+        // 处理带扩展名和不带扩展名的文件
+        String newName = (dotIndex > 0)
+                ? fileName.substring(0, dotIndex) + suffix + fileName.substring(dotIndex)
+                : fileName + suffix;
+
+        return originalPath.resolveSibling(newName);
+    }
 }