| | |
| | | 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; |
| | |
| | | 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; |
| | | } |