Example usage for org.apache.commons.vfs.provider.sftp SftpFileSystemConfigBuilder setStrictHostKeyChecking

List of usage examples for org.apache.commons.vfs.provider.sftp SftpFileSystemConfigBuilder setStrictHostKeyChecking

Introduction

In this page you can find the example usage for org.apache.commons.vfs.provider.sftp SftpFileSystemConfigBuilder setStrictHostKeyChecking.

Prototype

public void setStrictHostKeyChecking(FileSystemOptions opts, String hostKeyChecking)
        throws FileSystemException 

Source Link

Document

configure the host key checking to use.
valid arguments are only yes, no and ask.
See the jsch documentation for details.

Usage

From source file:com.learningobjects.community.abgm.logic.ControllerJob.java

private void download(String sourceUrl, File destinationFile) throws IOException {
    logger.log(Level.INFO, "Downloading or copying file from " + sourceUrl + " to " + destinationFile);
    FileObject sourceFileObject = null;
    OutputStream outputStream = null;
    try {//  w ww . ja  v  a 2 s  .  c  om
        // special case sftp so that new hosts work out of the box. other options could go here too
        SftpFileSystemConfigBuilder sftpFileSystemConfigBuilder = SftpFileSystemConfigBuilder.getInstance();
        FileSystemOptions fileSystemOptions = new FileSystemOptions();
        sftpFileSystemConfigBuilder.setStrictHostKeyChecking(fileSystemOptions, "no");
        // actually try to get the file
        FileSystemManager fsManager = VFS.getManager();
        sourceFileObject = fsManager.resolveFile(sourceUrl, fileSystemOptions);
        FileContent sourceFileContent = sourceFileObject.getContent();
        InputStream inputStream = sourceFileContent.getInputStream();
        outputStream = new FileOutputStream(destinationFile);
        // do the copy - this is probably a dupe of commons io, and many others
        byte[] buffer = new byte[8192];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
        if (sourceFileObject != null) {
            sourceFileObject.close(); // this will close the fileContent object, too
        }
    }
}