TCRISP
TCRISP

Reputation: 31

Using Apache Commons VFS- SFTP, uploading to a server

I'm attempting to use Apache Commons VFS to SFTP a file onto a server, but I keep getting the following error:

java.lang.RuntimeException: org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server at "sftp://user:***@xxx.x.xxx.xxx/".

Is it normal for it to not include the remote file path (remoteFilePath) here? It's in my code to include it in the connection string (see below)

I have the following jars included in my pom:

Code:

public void SftpMethod(String strMsg, String tableName){

    String host = "xxx.x.xxx.xxx";
    String user = "user";
    String pass = "password!";
    String localFilePath = "C:\\Users\\exampleDir\\Desktop\\loc.dat";
    String remoteFilePath = "/dir/home/user/export/loc.dat";
    StandardFileSystemManager manager = new StandardFileSystemManager();

    File file = new File(localFilePath);

    if (!file.exists())
        throw new RuntimeException("Error. Local file not found");

    try{
        manager.init();
        // Create local file object
        FileObject localFile = manager.resolveFile(file.getAbsolutePath());
        // Create remote file object
        FileObject remoteFile = manager.resolveFile(
                createConnectionString(host, user, pass, remoteFilePath), 
                createDefaultOptions());
        // Copy local file to SFTP server
        remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
        System.out.println("File upload success");

    }catch(IOException e){
        throw new RuntimeException(e);
    }finally{
        manager.close();
    }
}

public static String createConnectionString(String hostName, String username, String password, String remoteFilePath) {
    return "sftp://" + username + ":" + password + "@" + hostName + "/" + remoteFilePath;
}

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().setTimeout(opts, 10000);

    return opts;
}

Upvotes: 3

Views: 4528

Answers (1)

Aaron L. Johnson
Aaron L. Johnson

Reputation: 191

Without the full stack trace its hard to give a conclusive answer, but this is what I saw recently:

Caused by: org.apache.commons.vfs2.FileSystemException: Could not load private key from "/Users/<user>/.ssh/id_rsa".
    at org.apache.commons.vfs2.provider.sftp.SftpClientFactory.createConnection(SftpClientFactory.java:131)

Unfortunately, I wasn't trying to use a public/private key. I was only intending to log in with a username/password. I needed a way for it to stop trying to read my private key.

The root cause was that the code was using a default location for my key, and attempting to read it (even though thats not what I wanted).

So the workaround was to override the default location by setting the following property:

 System.setProperty("vfs.sftp.sshdir", "/");

This bypassed the attempt to read the ssh key altogether, and successfully connected.

Upvotes: 2

Related Questions