Example usage for org.apache.commons.vfs2 Selectors SELECT_SELF

List of usage examples for org.apache.commons.vfs2 Selectors SELECT_SELF

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 Selectors SELECT_SELF.

Prototype

FileSelector SELECT_SELF

To view the source code for org.apache.commons.vfs2 Selectors SELECT_SELF.

Click Source Link

Document

A FileSelector that selects only the base file/folder.

Usage

From source file:org.ysb33r.groovy.vfsplugin.cpio.CpioFileSystem.java

protected CpioFileSystem(final AbstractFileName rootName, final FileObject parentLayer,
        final FileSystemOptions fileSystemOptions) throws FileSystemException {
    super(rootName, parentLayer, fileSystemOptions);

    // Make a local copy of the file
    file = parentLayer.getFileSystem().replicateFile(parentLayer, Selectors.SELECT_SELF);

    // Open the Cpio file
    if (!file.exists()) {
        // Don't need to do anything
        cpioFile = null;/*from   w w w  . ja  va 2s . c o  m*/
        return;
    }

    // cpioFile = createCpioFile(this.file);
}

From source file:sftpexamples.GetMyFiles.java

public boolean startFTP(String propertiesFilename, String fileToDownload) {

    props = new Properties();
    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {/*from ww w . j  a  v  a  2s. c om*/

        props.load(new FileInputStream(System.getProperty("user.home") + propertiesFilename));
        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();

        //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 
        //      + "/" + remoteDirectory + fileToDownload;
        String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress + "/" + fileToDownload;
        // Create local file object
        String filepath = localDirectory + fileToDownload;
        File file = new File(filepath);
        FileObject localFile = manager.resolveFile(file.getAbsolutePath());

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

        // Copy local file to sftp server
        localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
        System.out.println("File download successful");

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

    return true;
}

From source file:sftpexamples.SendMyFiles.java

public boolean startFTP(String propertiesFilename, String fileToFTP) {

    props = new Properties();
    StandardFileSystemManager manager = new StandardFileSystemManager();
    try {/*from  w  w  w.  j a  v a2 s. c  o  m*/
        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//ww  w  .  java  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/*  ww  w  .  java  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  ww .ja v a  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  ww  .ja  va 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 an 'rm' command.//from  ww  w  .  j  a  v a  2  s  . c  o  m
 */
private void rm(final String[] cmd) throws Exception {

    if (cmd.length < 2) {
        throw new Exception("USAGE: rm <path>");
    }

    final FileObject file = mgr.resolveFile(cwd, cmd[1]);
    file.delete(Selectors.SELECT_SELF);
}