Example usage for org.apache.commons.vfs2.impl StandardFileSystemManager close

List of usage examples for org.apache.commons.vfs2.impl StandardFileSystemManager close

Introduction

In this page you can find the example usage for org.apache.commons.vfs2.impl StandardFileSystemManager close.

Prototype

public void close() 

Source Link

Document

Closes the manager.

Usage

From source file:sftpexamples.SftpUtility.java

/**
 * Method to upload a file in Remote server
 *
 * @param hostName HostName of the server
 * @param username UserName to login//from ww w.  j a va 2  s.co  m
 * @param password Password to login
 * @param localFilePath LocalFilePath. Should contain the entire local file
 * path - Directory and Filename with \\ as separator
 * @param remoteFilePath remoteFilePath. Should contain the entire remote
 * file path - Directory and Filename with / as separator
 */
public static void upload(String hostName, String username, String password, String localFilePath,
        String remoteFilePath) {

    File file = new File(localFilePath);
    if (!file.exists()) {
        throw new RuntimeException("Error. Local file not found");
    }

    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {
        manager.init();

        // Create local file object
        FileObject localFile = manager.resolveFile(file.getAbsolutePath());

        // Create remote file object
        FileObject remoteFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions());
        /*
         * use createDefaultOptions() in place of fsOptions for all default
         * options - Ashok.
         */

        // Copy local file to sftp server
        remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);

        System.out.println("File upload success");
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        manager.close();
    }
}

From source file:sftpexamples.SftpUtility.java

public static boolean move(String hostName, String username, String password, String remoteSrcFilePath,
        String remoteDestFilePath) {
    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {/*ww  w.jav a2  s  .c om*/
        manager.init();

        // Create remote object
        FileObject remoteFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteSrcFilePath),
                createDefaultOptions());
        FileObject remoteDestFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteDestFilePath),
                createDefaultOptions());

        if (remoteFile.exists()) {
            remoteFile.moveTo(remoteDestFile);
            ;
            System.out.println("Move remote file success");
            return true;
        } else {
            System.out.println("Source file doesn't exist");
            return false;
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        manager.close();
    }
}

From source file:sftpexamples.SftpUtility.java

/**
 * Method to download the file from remote server location
 *
 * @param hostName HostName of the server
 * @param username UserName to login//w w  w. ja  va2  s .  c  o m
 * @param password Password to login
 * @param localFilePath LocalFilePath. Should contain the entire local file
 * path - Directory and Filename with \\ as separator
 * @param remoteFilePath remoteFilePath. Should contain the entire remote
 * file path - Directory and Filename with / as separator
 */
public static void download(String hostName, String username, String password, String localFilePath,
        String remoteFilePath) {

    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {
        manager.init();

        // Append _downlaod_from_sftp to the given file name.
        //String downloadFilePath = localFilePath.substring(0, localFilePath.lastIndexOf(".")) + "_downlaod_from_sftp" + localFilePath.substring(localFilePath.lastIndexOf("."), localFilePath.length());
        // Create local file object. Change location if necessary for new downloadFilePath
        FileObject localFile = manager.resolveFile(localFilePath);

        // Create remote file object
        FileObject remoteFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions());

        // Copy local file to sftp server
        localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);

        System.out.println("File download success");
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        manager.close();
    }
}

From source file:sftpexamples.SftpUtility.java

/**
 * Method to delete the specified file from the remote system
 *
 * @param hostName HostName of the server
 * @param username UserName to login//from   w  ww  .ja  v  a2s.com
 * @param password Password to login
 * @param localFilePath LocalFilePath. Should contain the entire local file
 * path - Directory and Filename with \\ as separator
 * @param remoteFilePath remoteFilePath. Should contain the entire remote
 * file path - Directory and Filename with / as separator
 */
public static void delete(String hostName, String username, String password, String remoteFilePath) {
    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {
        manager.init();

        // Create remote object
        FileObject remoteFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions());

        if (remoteFile.exists()) {
            remoteFile.delete();
            System.out.println("Delete remote file success");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        manager.close();
    }
}

From source file:sftpexamples.SftpUtility.java

/**
 * Method to check if the remote file exists in the specified remote
 * location/*from w  w w . j  a  v  a  2 s. c o  m*/
 *
 * @param hostName HostName of the server
 * @param username UserName to login
 * @param password Password to login
 * @param remoteFilePath remoteFilePath. Should contain the entire remote
 * file path - Directory and Filename with / as separator
 * @return Returns if the file exists in the specified remote location
 */
public static boolean exist(String hostName, String username, String password, String remoteFilePath) {
    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {
        manager.init();

        // Create remote object
        FileObject remoteFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions());

        System.out.println("File exist: " + remoteFile.exists());

        return remoteFile.exists();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        manager.close();
    }
}

From source file:si.matjazcerkvenik.dtools.tools.ftp.VfsFtpSftpClient.java

public void upload(String localFile, String remoteFile) {

    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {//from   w  w w  . j  a v a  2s.com

        // check if the file exists
        File file = new File(localFile);
        if (!file.exists())
            throw new RuntimeException("Error. Local file not found");

        // Initializes the file manager
        manager.init();

        // Setup our SFTP configuration
        FileSystemOptions opts = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
        SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

        System.out.println("copy " + file.getAbsolutePath() + " to");
        // Create the SFTP URI using the host name, userid, password, remote
        // path and file name
        String sftpUri = protocol + "://" + username + ":" + password + "@" + hostname + ":" + port
                + remoteFile;
        System.out.println(sftpUri);

        // Create local file object
        FileObject localFileObject = manager.resolveFile(file.getAbsolutePath());

        // Create remote file object
        FileObject remoteFileObject = manager.resolveFile(sftpUri, opts);

        // Copy local file to sftp server
        remoteFileObject.copyFrom(localFileObject, Selectors.SELECT_SELF);
        System.out.println("File upload successful");

    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        manager.close();
    }

}

From source file:si.matjazcerkvenik.dtools.tools.ftp.VfsFtpSftpClient.java

public void download(String localFile, String remoteFile) {

    StandardFileSystemManager manager = new StandardFileSystemManager();
    // Initializes the file manager
    try {/*from   w ww .j av a 2 s . com*/
        manager.init();
        // Setup our SFTP configuration
        FileSystemOptions opts = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
        SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

        // Create the SFTP URI using the host name, userid, password, remote
        // path and file name
        String sftpUri = protocol + "://" + username + ":" + password + "@" + hostname + ":" + port
                + remoteFile;
        System.out.println(sftpUri);

        // Create local file object
        File file = new File(localFile);
        FileObject localFileObject = manager.resolveFile(file.getAbsolutePath());

        // Create remote file object
        FileObject remoteFileObject = manager.resolveFile(sftpUri, opts);

        // Copy local file to sftp server
        localFileObject.copyFrom(remoteFileObject, Selectors.SELECT_SELF);
        System.out.println("File download successful");
    } catch (FileSystemException e) {
        e.printStackTrace();
    } finally {
        manager.close();
    }

}