如何使用 apache commons vfs 读取从 Windows 到 Linux 的远程文件?

How to read remote file from Windows to Linux with apache commons vfs?

我在 Windows 有一个文件,我想从 linux 读取这个文件。当我尝试从 Windows 运行 它时,下面的代码工作正常但是当我尝试从 linux 运行 它给出;

"Could not read from "file:///10.0.0.1/C$/myfolder/test.txt" because it is not a file."

这是我的代码;

    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    FileObject fo = VFS.getManager().resolveFile(remoteFilePath, opts);

    InputStream inputStream = fo.getContent().getInputStream(); //this line throws exception

我想我可能有适合您的解决方案。

我有同样的错误并通过设置如下选项解决了它:

private void readSFTP(){        
// ...
final FileObject sftpOriginObj = manager.resolveFile(ftpOrigin, getSFTPOptions());
// ...
}

    private static FileSystemOptions getSFTPOptions() throws FileSystemException {
            // Create SFTP options
            final 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, false);

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

            return opts;
        }

希望对您有所帮助。