java - Apache commons VFS 未知方案文件

标签 java apache-commons-vfs

我什至去了谷歌第 5 页,但没有答案... 我有以下内容(我检查过 war ,它也在那里):

commons-collections4-4.0.jar
commons-io-2.4.jar
commons-lang3-3.3.2.jar
commons-logging-1.2.jar
commons-net-3.3.jar
commons-vfs2-2.0.jar
jackrabbit-standalone-2.8.0.jar
javax.mail-1.4.4.jar
jcifs-1.3.18.jar
jsch-0.1.51.jar
primefaces-5.0.jar

我想 1) 创建一个本地文件和 2) 通过 sftp 将该文件复制到我的服务器。该文件在正确的目录中本地创建,具有正确的数据。

我的代码被截断了:

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystem;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.UserAuthenticator;
import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;

...

public void dosftp(String data) throws FileSystemException
{
    String username = "username";
    String password = "password";
    String fileName = "test.txt";
    String localPath = "C:\\Temp\\";
    String fullPath = "sftp://xxx.xxx.xxx.xxx:22/~/temp/" + fileName;
    StandardFileSystemManager fsManager = null;
    FileObject remoteFileObject = null;
    FileObject localFileObject = null;
    try
    {
        File file = new File(localPath + fileName);
        FileUtils.writeStringToFile(file, data);

        fsManager = new StandardFileSystemManager();
        FileSystemOptions opts = new FileSystemOptions();
        //Do auth stuff
        UserAuthenticator auth =  new StaticUserAuthenticator(null, username, password);
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
        try
        {
            URI uri = new URI(fullPath);
            if(StringUtils.equalsIgnoreCase(uri.getScheme(), "sftp"))
            {
                LOG.info("Scheme is sftp");                       
                SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
                SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
                SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
            }
            catch(URISyntaxException e)
            {
                LOG.log(Level.WARNING,"Error while checking file's uri", e);
            }
        }

        localFileObject = fsManager.resolveFile("file:///" + file.getAbsolutePath());
        remoteFileObject = fsManager.resolveFile(fullPath, opts);
        remoteFileObject.copyFrom(localFileObject, Selectors.SELECT_SELF);
    }
    catch (FileSystemException e)
    {
        LOG.log(Level.SEVERE, "Error while writing file out", e);
    }
    catch (IOException e)
    {
        LOG.log(Level.SEVERE, "Error while writing file out", e);
    }
    finally
    {
        if((fsManager != null) && (remoteFileObject != null))
        {
            FileSystem fs = null;
            remoteFileObject.close(); // Seems to still work even if this line is omitted
            fs = remoteFileObject.getFileSystem(); // This works even after the src is closed.
            fsManager.closeFileSystem(fs);
        }
        if((fsManager != null) && (localFileObject != null))
        {
            FileSystem fs = null;
            localFileObject.close(); // Seems to still work even if this line is omitted
            fs = localFileObject.getFileSystem(); // This works even after the src is closed.
            fsManager.closeFileSystem(fs);
        }
    }
}

如果我使用“file:///”,那么在解析本地文件时会出现以下异常:

org.apache.commons.vfs2.FileSystemException: Unknown scheme "file" in URI "file:///C:\Temp\test.txt".

如果我省略“file:///”,那么我会得到:

org.apache.commons.vfs2.FileSystemException: Could not find file with URI "C:\Temp\test.txt" because it is a relative path, and no base URI was provided.

如果我在 localFileObject 之前执行 remoteFileObject,那么我得到

org.apache.commons.vfs2.FileSystemException: Unknown scheme "sftp" in URI "sftp://xxx.xxx.xxx.xxx:22/~/temp/test.txt"

请帮忙。

最佳答案

您必须在 StandardFileSystemManager 实例上调用 init(),以便它从文件“providers.xml”(必须在您的类路径中)加载方案。

关于java - Apache commons VFS 未知方案文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28852926/

相关文章:

java - 配置pentaho的hdfs-vfs来获取hdfs-site.xml

java - 为什么 Apache Commons VFS 考虑 Http Proxy 和 Socks5 Proxy 而忽略 Socks4 Proxy?

java - 如何修复使用 Apache Commons VFS 将文件上传到 SFTP 服务器时发生的错误

java - 使用 Eclipse 远程调试 Tomcat - 忽略断点

java - 使用 Apache Commons VFS API 的特定文件监视器

java - 使用 FTP URL 调用 FileSystemManager.resolveFile 时无法更改到工作目录 "/"

java - 如何在Spring boot项目中实现google自动登录功能?

java - 有人用条纹框架试过 scala

java - 如何让 tomcat 同时托管 jenkins 和 sonar 而不会因 OutOfMemoryException 而崩溃?

java - 使用另一个表中出现的次数计算属性值?