【问题标题】:How to move (not copy) a file with JCIFS?如何使用 JCIFS 移动(而不是复制)文件?
【发布时间】:2016-02-11 03:31:46
【问题描述】:

我想知道如何使用 JCIFS 将文件从一个文件夹移动到 SMB 共享上的另一个文件夹。

首先,没有任何move() 方法。

那么,这个方法:

SmbFile smbFromFile = new SmbFile("smb://...pool/from-here/the-file.pdf", auth);
SmbFile smbToFile = new SmbFile("smb://...pool/to-here/the-file.pdf", auth);
smbFromFile.renameTo(smbToFile);

抛出异常,"The system cannot find the path specified."

重命名仅适用于同一路径。更改参数没有帮助。

现在,我正在使用

smbFromFile = new SmbFile("smb://...pool/from-here/the-file.pdf", auth);
smbToFile = new SmbFile("smb://...pool/to-here", auth);
smbFromFile.copyTo(smbToFile);
smbFromFile.delete();

这感觉有点不对。

不幸的是,在docu 中我没有找到任何关于移动文件的信息。

有人有更多的信息吗?它应该是 SMB 的一部分,对吧 (SMB_COM_MOVE)?

【问题讨论】:

    标签: smb jcifs


    【解决方案1】:

    原来我是个布偶,因为我搞砸了我的配置参数。

    两种方式都可以正常工作:

    方法一:

    SmbFile smbFromFile = new SmbFile("smb://...pool/from-here/the-file.pdf", auth);
    SmbFile smbToFile = new SmbFile("smb://...pool/to-here/the-file.pdf", auth);
    smbFromFile.renameTo(smbToFile); 
    

    方法二:

    smbFromFile = new SmbFile("smb://...pool/from-here/the-file.pdf", auth);
    smbToFile = new SmbFile("smb://...pool/to-here/the-file.pdf", auth);
    smbFromFile.copyTo(smbToFile);
    smbFromFile.delete();
    

    【讨论】:

    • 这只有在两个文件都在同一个 smb 资源上时才有效。
    【解决方案2】:

    有两种可能的场景:

    1.) 文件需要在同一个服务器上移动(即Input文件夹和Output文件夹的认证细节相同)。

    使用 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); //Use 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.) 文件需要在不同的服务器上移动(即输入文件夹和输出文件夹的认证细节是NOT 一样)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多