4

I've even went to google page #5, but no answer... I have the following included (I've check the war and it is also there):

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

I want to 1) create a local file and 2) copy that file via sftp to my server. The file gets locally created in the right directory, with the correct data.

My code snipped:

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);
        }
    }
}

If I use "file:///" then I get the following exception where I resolve the local file:

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

If I omit "file:///" then I get:

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.

If I do remoteFileObject before localFileObject, then I get

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

Please help.

2 Answers 2

5

You must call init() on the StandardFileSystemManager instance, so that it loads the schemes from the file "providers.xml" (that must be on your classpath).

1
4

You can initialize programmatically as well

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(currSchemeStr, new AzFileProvider());
    currMan.addProvider("file", new DefaultLocalFileProvider());
    currMan.init(); 

    currMan.resolveFile("file:///tmp" +  + file.getAbsolutePath());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.