JCIFS SmbFile RenameTo 无法移动文件

JCIFS SmbFile RenameTo fails to a move file

我在使用 JCIFS SmbFile.renameTo() 方法时有最奇怪的行为。当我执行下面的代码时,它应该将网络文件从 test1 移动到 test2,但它在 test2 中创建了一个名为 test.xml 的文件夹并抛出以下错误 "Cannot create a file when that file already exists..." 我无法弄清楚。为什么这个方法会这样?

 NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication (sDomain, 
                                       sUsername, sPassword);
 SmbFile smbFromFile = new SmbFile("smb://test1/test.xml", auth);
 SmbFile smbToFile = new SmbFile("smb://test2/test.xml", auth);
 smbFromFile.renameTo(smbToFile);

copyTo(SmbFile) and renameTo(SmbFile) - only one of them says This file and the destination file do not need to be on the same host. As renameTo(SmbFile) does not say that, I can only assume you should use copyTo and then delete() 与原版之间有一个有趣的区别。

SmbFile smbFromFile = new SmbFile("smb://test1/test.xml", auth);
SmbFile smbToFile = new SmbFile("smb://test2/test.xml", auth);
// smbFromFile.renameTo(smbToFile);
smbFromFile.copyTo(smbToFile);
smbFromFile.delete();

有两种可能的情况:

1.) 文件需要在同一台服务器上移动(即,输入文件夹和输出文件夹的身份验证详细信息相同)。

使用 renameTo() 方法。

 public boolean moveFile(SmbFile file) {
    log.info("{"Started Archiving or Moving the file");
    String targetFilePath = this.archiveDir + file.getName(); //Path where we need to move that file.
    try {
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", userId, userPassword);
        log.info("targetFilePath: {} , currentFile : {}",targetFilePath, file);
        SmbFile targetFile = new SmbFile(targetFilePath, auth); 
          //authenticate the SmbFile
        try {
            file.renameTo(targetFile); //User renameTo method for same server
            log.info("Archived File : {} to: {}", file.getName(), 
            targetFile.getName());
            return true;
        } catch (SmbException e) {
            log.error("Unable to Archive File: {}", file.getName());
            return false;
        }
    } catch (MalformedURLException e) {
        log.error("Connection failed to Server Drive: {}", targetFilePath);
    }
    return false;
}

2.) 文件需要在不同的服务器上移动(也就是说,输入文件夹和输出文件夹的身份验证详细信息是 相同)。

使用copyTo()方法。

这里我会建议,你可以先验证文件存在的第一台服务器,然后检查文件是否存在,如果存在则将其添加到列表中:

public List<SmbFile> xmlFiles = new ArrayList<>(); //Here we will add all the files which are existing.

public boolean isFileExists() throws MalformedURLException, SmbException {
  NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", 
  userID, userPassword); //authenticating input folder.
    SmbFile smbFile = new SmbFile(inputFolder, auth);
    SmbFile[] smbFiles = smbFile.listFiles();
    boolean isFilePresent = false;
    if (smbFiles.length > 0) {
        for (SmbFile file : smbFiles) {
            if (file.getName().toLowerCase(Locale.ENGLISH)              
       .contains(AppConstant.FILE_NAME.toLowerCase(Locale.ENGLISH))) {
                xmlFiles.add(file);
                isFilePresent = true;
            }
        }
    }
    if (isPlanFilePresent) {
        log.info("Number of files present on Server: {}",smbFiles.length);
        return true;
    }
    return false;
}

这将为您提供列表中的文件。继续将其复制到另一台服务器。请注意,您需要在此处对输出文件夹进行身份验证。

 public boolean moveFile(SmbFile file) {
    log.info("Started Moving or Archiving the file");
    String toFilePath = this.outputFolder + file.getName(); //path where you need to copy the file from input folder.
    try {
        NtlmPasswordAuthentication auth1 = new NtlmPasswordAuthentication("", outputFolderUserId, outputFolderPassword); //authenticating output folder
        log.info("targetFilePath: {} and currentFile : {}", toFilePath, file);
        SmbFile targetFile = new SmbFile(toFilePath, auth1);
      
        try {
            file.copyTo(targetFile);
            file.delete(); //delete the file which we copied at our desired server
            log.info("Archived File : {} to: {}", file.getName(), targetFile.getName());
            return true;
        } catch (SmbException e) {
            log.error("Unable to Archive File: {}", file.getName());
            return false;
        }

    } catch (MalformedURLException e) {
        log.error("Connection failed to Server Drive: {}", toFilePath);
    }
    return false;
}

乐于助人 :)