Apache commons VFS未知方案文件



我甚至去了谷歌第5页,但没有答案。。。我有以下内容(我已经查看了战争,它也在那里):

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:Temptest.txt".

如果我省略了"file:///",那么我得到:

org.apache.commons.vfs2.FileSystemException: Could not find file with URI "C:Temptest.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"(必须在类路径上)加载方案。

您也可以通过编程方式初始化

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(currSchemeStr, new AzFileProvider());
    currMan.addProvider("file", new DefaultLocalFileProvider());
    currMan.init(); 
    currMan.resolveFile("file:///tmp" +  + file.getAbsolutePath());

相关内容

  • 没有找到相关文章

最新更新