当前位置: 首页 > 知识库问答 >
问题:

如何使用JCIFS移动(而不是复制)文件?

申高峯
2023-03-14

我想知道如何使用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);

引发异常,“系统找不到指定的路径。”

重命名仅在同一路径中有效。改变参数也无济于事。

现在,我正在使用

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

这感觉有点不对劲。

不幸的是,在文档中我没有找到任何关于移动文件的东西。

有人有更多的信息吗?它应该是SMB的一部分,对吗(SMB\u COM\u MOVE)?


共有2个答案

查飞星
2023-03-14

有两种可能的情况:

1.)文件需要在同一台服务器上移动(也就是说,输入文件夹和输出文件夹的身份验证细节是相同的)。

使用rename()方法。

 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.)文件需要在不同的服务器上移动(即,输入文件夹和输出文件夹的身份验证详细信息不同)。

使用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;
}
范承教
2023-03-14

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

这两种方法都很有效:

方法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); 

方法2:

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();
 类似资料:
  • 试图从jcifs转移到jcifs-ng(最新的jar jcifs-ng-2.1.2.jar)以将文件复制到/从远程。我使用旧JCIFS的代码: 在stackoverflow中找到的示例很少,但看起来它们已经过时了。

  • 有人能帮我把文件从共享文件夹复制到本地驱动器吗?我的代码是: 文件未被复制。我收到一条消息“无法连接到服务器”,但程序显示dir。源文件的getDate()(以及文件名和长度)。因此,我认为目标文件夹(C:/SQLRESTORESTAGE/)存在问题。此外,我也提供了只用于读取源文件。你能帮我纠正一下密码或者给点建议吗?非常感谢。

  • 问题内容: 我正在尝试使用Hibernate连接到servlet中的DB。我读到我们可以使用hibernate.cfg.xml或hibernate.properties文件来配置会话。对我来说,它可以与xml一起使用。现在,当我尝试使用属性而不是xml时,它不起作用。据说 没有找到* hibernate.cfg.xml 。但是我没有提到要使用xml文件,事实上我已经删除了该xml文件。 * 请帮我

  • 我想我可以使用smbfile.copyto(),但我不知道如何访问本地文件。如果我写了以下内容,我会得到一个连接错误: 这个问题与如何在Java中使用jcifs将文件从smb共享复制到本地驱动器有关?

  • 问题内容: 我只是在测试JCIFS以访问Windows共享。完全无法使用的速度非常慢。 初始输出需要很长时间,后续读取也很慢。任何想法如何使用它?我也可以使用任何其他可替代的方式来编写Java代码来以可移植的方式访问Windows共享。 问题答案: 我在某处发现SmbFileInputStream不会自己进行缓冲,因此很慢。将SmbFileInputStream包裹在BufferedInputSt

  • 我有这样的文字: : wink: and:赞:是显示表情符号的代码。现在我使用拆分来隐蔽成这样的数组: 结果: 如您所见,字符“:”已消失,如何在从文本转换为数组时保持“:”,在这种情况下,我希望结果应该是: