Example usage for org.apache.commons.vfs2 FileObject copyFrom

List of usage examples for org.apache.commons.vfs2 FileObject copyFrom

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject copyFrom.

Prototype

void copyFrom(FileObject srcFile, FileSelector selector) throws FileSystemException;

Source Link

Document

Copies another file, and all its descendants, to this file.

Usage

From source file:sftpexamples.SendMyFiles.java

public boolean startFTP(String propertiesFilename, String fileToFTP) {

    props = new Properties();
    StandardFileSystemManager manager = new StandardFileSystemManager();
    try {/*w w w. j ava2 s .com*/
        props.load(new FileInputStream(System.getProperty("user.home") + propertiesFilename));
        //props.setProperty("serverAddress", "127.0.0.1");
        String serverAddress = props.getProperty("serverAddress").trim();
        String userId = props.getProperty("userId").trim();
        String password = props.getProperty("password").trim();
        String remoteDirectory = props.getProperty("remoteDirectory").trim();
        String localDirectory = props.getProperty("localDirectory").trim();
        System.out.println("properties are fetched:serverAddress=" + serverAddress);
        System.out.println("userId=" + userId);
        System.out.println("password=" + password);
        System.out.println("remoteDirectory=" + remoteDirectory);
        System.out.println("localDirectory=" + localDirectory);
        //check if the file exists
        String filepath = localDirectory + fileToFTP;
        System.out.println("filepath:" + filepath);
        File file = new File(filepath);
        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);

        //Create the SFTP URI using the host name, userid, password,  remote path and file name
        String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress + "/" + fileToFTP;
        System.out.println("uri: " + sftpUri);
        // Create local file object
        FileObject localFile = manager.resolveFile(file.getAbsolutePath());

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

        // Copy local file to sftp server
        remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
        System.out.println("File upload successful");
    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    } finally {
        manager.close();
    }
    return true;
}

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/*  w w w.  j a  v a 2s  .  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 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

/**
 * Method to download the file from remote server location
 *
 * @param hostName HostName of the server
 * @param username UserName to login/*from  w  ww .j  a v a 2s  . c om*/
 * @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: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 va 2  s  .  c om*/

        // 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  w  w  .j  a v  a  2  s . c om*/
        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();
    }

}

From source file:tain.kr.test.vfs.v01.Shell.java

/**
 * Does a 'cp' command.//from  w ww.  j  a v  a2s  .c om
 */
private void cp(final String[] cmd) throws Exception {

    if (cmd.length < 3) {
        throw new Exception("USAGE: cp <src> <dest>");
    }

    final FileObject src = mgr.resolveFile(cwd, cmd[1]);
    FileObject dest = mgr.resolveFile(cwd, cmd[2]);

    if (dest.exists() && dest.getType() == FileType.FOLDER) {
        dest = dest.resolveFile(src.getName().getBaseName());
    }

    dest.copyFrom(src, Selectors.SELECT_ALL);
}

From source file:tw.edu.sinica.iis.SSHadoop.SSHSftp.java

public boolean sftpUpload(final String localDir, final String remoteDir) {
    try {//from  w  ww . j  a v  a 2  s  . co m
        manager.init();

        FileObject local = manager.resolveFile(localDir);
        FileObject remote = manager.resolveFile(genConnectString() + remoteDir, opts.getOptions());

        remote.copyFrom(local, Selectors.SELECT_ALL);

    } catch (FileSystemException e) {
        e.printStackTrace();
        return false;
    } finally {
        manager.close();
    }
    return true;
}

From source file:tw.edu.sinica.iis.SSHadoop.SSHSftp.java

public void sftpDownload(final String remoteDir, final String localDir) {
    try {/*  w ww  .  ja v a  2 s  .co  m*/
        manager.init();

        FileObject local = manager.resolveFile(localDir);
        FileObject remote = manager.resolveFile(genConnectString() + remoteDir, opts.getOptions());

        local.copyFrom(remote, Selectors.SELECT_ALL);

    } catch (FileSystemException e) {
        e.printStackTrace();
    } finally {
        manager.close();
    }
}