|
@@ -17,6 +17,7 @@ import org.springblade.common.constant.CommonConstant;
|
|
|
import org.springblade.common.utils.CommonUtil;
|
|
|
import org.springblade.common.utils.SystemUtils;
|
|
|
import org.springblade.common.vo.DataVO;
|
|
|
+import org.springblade.core.log.exception.ServiceException;
|
|
|
import org.springblade.core.tool.utils.IoUtil;
|
|
|
import org.springblade.system.cache.ParamCache;
|
|
|
|
|
@@ -31,6 +32,7 @@ import java.awt.image.AffineTransformOp;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.nio.file.*;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
@@ -366,4 +368,81 @@ public class FileUtils {
|
|
|
String path = sys_file_net_url + fileUrl.replaceAll("//", "/").replaceAll(file_path2, "");
|
|
|
return path;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件绝对路径并复制文件
|
|
|
+ * @param sourcePath 源文件路径(相对或绝对)
|
|
|
+ */
|
|
|
+ public static String copyFileWithAbsolutePath(String sourcePath) {
|
|
|
+ try {
|
|
|
+ // 1. 获取源文件的绝对路径
|
|
|
+ File sourceFile = new File(sourcePath);
|
|
|
+ String absolutePath = sourceFile.getAbsolutePath();
|
|
|
+ System.out.println("源文件绝对路径: " + absolutePath);
|
|
|
+
|
|
|
+ // 2. 验证文件存在且可读
|
|
|
+ if (!sourceFile.exists()) {
|
|
|
+ System.err.println("错误: 源文件不存在");
|
|
|
+ throw new ServiceException("错误: 源文件不存在");
|
|
|
+ }
|
|
|
+ if (!sourceFile.canRead()) {
|
|
|
+ System.err.println("错误: 无法读取源文件");
|
|
|
+
|
|
|
+ throw new ServiceException("错误: 源文件不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 创建目标文件路径(在相同目录,文件名添加 _copy)
|
|
|
+ String parentDir = sourceFile.getParent();
|
|
|
+ String fileName = sourceFile.getName();
|
|
|
+
|
|
|
+ // 提取文件名和扩展名
|
|
|
+ String baseName;
|
|
|
+ String extension = "";
|
|
|
+ int dotIndex = fileName.lastIndexOf('.');
|
|
|
+ if (dotIndex > 0) {
|
|
|
+ baseName = fileName.substring(0, dotIndex);
|
|
|
+ extension = fileName.substring(dotIndex);
|
|
|
+ } else {
|
|
|
+ baseName = fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成唯一的目标文件名(避免覆盖)
|
|
|
+ File destFile;
|
|
|
+ int copyCounter = 1;
|
|
|
+ do {
|
|
|
+ String newFileName = baseName + "_copy" +
|
|
|
+ (copyCounter > 1 ? "_" + copyCounter : "") +
|
|
|
+ extension;
|
|
|
+ destFile = new File(parentDir, newFileName);
|
|
|
+ copyCounter++;
|
|
|
+ } while (destFile.exists());
|
|
|
+
|
|
|
+ System.out.println("目标文件路径: " + destFile.getAbsolutePath());
|
|
|
+
|
|
|
+ // 4. 执行文件复制(使用NIO方法)
|
|
|
+ Path source = sourceFile.toPath();
|
|
|
+ Path destination = destFile.toPath();
|
|
|
+
|
|
|
+ // 复制文件并保留属性
|
|
|
+ Files.copy(source, destination,
|
|
|
+ StandardCopyOption.COPY_ATTRIBUTES,
|
|
|
+ StandardCopyOption.REPLACE_EXISTING);
|
|
|
+
|
|
|
+ System.out.println("文件复制成功!");
|
|
|
+ return destFile.getAbsolutePath();
|
|
|
+ } catch (InvalidPathException e) {
|
|
|
+ System.err.println("路径无效: " + e.getMessage());
|
|
|
+ throw new ServiceException("路径无效");
|
|
|
+ } catch (FileAlreadyExistsException e) {
|
|
|
+ System.err.println("文件已存在: " + e.getMessage());
|
|
|
+ throw new ServiceException("文件已存在");
|
|
|
+ } catch (AccessDeniedException e) {
|
|
|
+ System.err.println("访问被拒绝: " + e.getMessage());
|
|
|
+ throw new ServiceException("访问被拒绝");
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.err.println("IO错误: " + e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new ServiceException("IO错误");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|