|
@@ -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);
|
|
|
+ }
|
|
|
}
|