Home > Java > using a combination of JSCH and Apache VFS Commons to SFTP with a private key

using a combination of JSCH and Apache VFS Commons to SFTP with a private key


package test.sftp;

import java.io.File;

import org.apache.commons.vfs.AllFileSelector;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder;

import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;

public class SFTPTest {

public static void main(String[] args) throws JSchException, SftpException,
FileSystemException {

FileSystemOptions options = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(options, "no");
File keyFile = new File("path/to/the/private/key.ppk");
SftpFileSystemConfigBuilder.getInstance().setIdentities(options, new File[] { keyFile });
SftpFileSystemConfigBuilder.getInstance().setKnownHosts(options, new File("path/to/known_hosts")); // optional step, usually ~/.ssh/known_hosts

FileSystemManager fsManager = VFS.getManager();
String uri = "sftp://username:password@hostname:22";

FileObject from = fsManager.resolveFile(uri, options);
FileObject to = fsManager.resolveFile("/tmp", options);
to.copyFrom(from, new AllFileSelector());
}
}

Categories: Java Tags: , , , , , , , , ,
  1. April 27, 2018 at 4:06 pm

    Your view on binary differs from the vast majority of websites I go through,
    I’m pleased.

  2. July 24, 2018 at 12:59 am

    I benefit from your suggestions on options, you own a specific view that
    i am not used to. Superb.

  3. July 26, 2018 at 5:39 am

    Let’s feature a conversation some time around
    binary options trading along with what we can do to ensure it is more effective for everybody.

  4. August 7, 2018 at 9:53 am

    Let’s feature a conversation some time with regards to options
    trading and what we’ll be able to do to ensure that it
    is more effective for everybody.

  5. August 12, 2018 at 12:06 pm

    Your look at binary differs from almost all of
    the blog posts I go through, I’m satisfied.

  6. September 9, 2018 at 5:31 am

    Your approach to binary options trading differs from
    the majority of the websites I read, I’m impressed.

  1. No trackbacks yet.

Leave a comment