java - 无法在 ubuntu 18.04 中使用 apache commons vfs for vsftpd 重命名文件

标签 java sftp ubuntu-18.04 apache-commons-vfs vsftpd

我正在尝试使用 apache commons vfs 重命名 vsftpd 服务器中的文件,moveTo 函数在本地系统操作系统(Kubuntu 19.04)和 VSFTPD 服务器中工作正常,但是当我尝试在具有 ubuntu 18.04 的测试环境中重命名该文件时我无法重命名出现异常的文件。

使用此代码:

    public static boolean move(String hostName, String username, String password, String remoteSrcFilePath,
        String remoteDestFilePath, byte [] data) {

    FileObject remoteFile = null;
    FileObject remoteDestFile = null;
    boolean result = false;

    try (StandardFileSystemManager manager = new StandardFileSystemManager()){
        manager.init();

        // Create remote object
        remoteFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteSrcFilePath), createDefaultOptions());
        remoteDestFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteDestFilePath), createDefaultOptions());

        if (!remoteDestFile.exists() && remoteFile.exists()) {
            remoteFile.moveTo(remoteDestFile);
            if(null != data)
                writeData(remoteDestFile, data);
            result =  true;
        }else {
            throw new DataIntegrityViolationException("Destination path already exists");
        }
    } catch (IOException e) {
        logger.error("Error while renaming/moving file",e);
    }  finally {
        try {
            if(null != remoteDestFile)
                remoteDestFile.close();

            if(null != remoteFile)
                remoteFile.close();

        } catch (FileSystemException e) {
            logger.warn("error while closing fileobject "+e.getMessage());
        }
    }
    return result;
}

    public static FileSystemOptions createDefaultOptions() throws FileSystemException {
    // Create SFTP options
    FileSystemOptions opts = new FileSystemOptions();

    // SSH Key checking
    SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");

    /*
     * Using the following line will cause VFS to choose File System's Root as VFS's
     * root. If I wanted to use User's home as VFS's root then set 2nd method
     * parameter to "true"
     */
    // Root directory set to user home
    SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);

    // Timeout is count by Milliseconds
    SftpFileSystemConfigBuilder.getInstance().setConnectTimeoutMillis(opts, 10000);

    return opts;
    }
}

我有这个异常(exception)

org.apache.commons.vfs2.FileSystemException: Could not determine if file "sftp://ftpuser:***@ip_address/home/ftpuser/ftp/1/Documents/test1/test2" is writeable

请注意,上面的代码在本地运行得很好。

最佳答案

如果您遵循source code for apache commons vfs for moveTo()你会发现:

if (canRenameTo(destFile)) {            //src and dest have the same FS
  if (!getParent().isWriteable()) {     // <-- it could throw the same exception here
    throw new FileSystemException("vfs.provider/rename-parent-read-only.error", getName(),
                    getParent().getName());
  }
} else {
  if (!isWriteable()) {    // <---- it throws inside here (IMO) rather than returning false 
    throw new FileSystemException("vfs.provider/rename-read-only.error", getName());
  }
}

...,您会发现,如果目标文件不可写,但以灾难性的方式(如 isWriteable( file) 在其主体内部抛出异常,而不是返回 false

我的第一个调用是验证 sftpd 进程ftp 用户 是否可以写入您想要将文件移动到的目录。

呵呵!

关于java - 无法在 ubuntu 18.04 中使用 apache commons vfs for vsftpd 重命名文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58246588/

相关文章:

python - paramiko 中的 ftplib retrbinary?

perl - 如何在 Perl 中使用 NET::SSH2 的 SFTP 模块按时间顺序列出文件?

couchbase-cli : command not found

尽管文件已上传,Python pysftp.put 仍引发 "No such file"异常

java - 在 jdbc 连接字符串中设置实例名称没有效果

JavaFX WebView/WebEngine 缓存外部 JavaScript

java - 将访问例程添加到 JAXB 生成的源自 XSD 的类集

python - 从源代码安装opencv时,如何在pyinstaller中包含OpenCV?

mysql - 将 MySQL 5.7 更新到 MySQL 8.0

java - 由二进制 XML 文件引起的 IllegalArgumentException (Android)