Search in sources :

Example 1 with SftpFileSystemConfigBuilder

use of com.jn.agileway.vfs.provider.sftp.SftpFileSystemConfigBuilder in project agileway by fangjinuo.

the class AgilewaySftpProviderTests method testListChildren.

@Test
public void testListChildren() throws Throwable {
    DefaultFileSystemManager fileSystemManager = (DefaultFileSystemManager) VFS.getManager();
    String url = "sftp://fangjinuo:fjn13570@192.168.1.70:22/test2/vfs_sftp_test";
    FileSystemOptions fileSystemOptions = new FileSystemOptions();
    SftpFileSystemConfigBuilder configBuilder = SftpFileSystemConfigBuilder.getInstance();
    configBuilder.setUserDirIsRoot(fileSystemOptions, true);
    FileObject fileObject = fileSystemManager.resolveFile(url, fileSystemOptions);
    System.out.println("===============Show remote directory==============");
    showFile(0, fileObject);
    System.out.println("===============Copy remote files to local==============");
    url = "file://d:/tmp002";
    FileObject localFileObject = fileSystemManager.resolveFile(url);
    if (fileObject.isFolder()) {
        if (!localFileObject.exists()) {
            localFileObject.createFolder();
        } else {
            localFileObject.delete(Selectors.EXCLUDE_SELF);
        }
        localFileObject.copyFrom(fileObject, Selectors.EXCLUDE_SELF);
    } else {
        // 单独测试 文件时,将上面的 url 改成一个 文件的url即可
        long writeSize = fileObject.getContent().write(localFileObject);
        long expectedSize = fileObject.getContent().getSize();
        Preconditions.checkTrue(writeSize == expectedSize);
        localFileObject.copyFrom(fileObject, Selectors.SELECT_SELF);
    }
    System.out.println("================Copy local files to remote=============");
    url = "sftp://fangjinuo:fjn13570@192.168.1.70:22/test2/vfs_sftp_test2";
    fileObject = fileSystemManager.resolveFile(url, fileSystemOptions);
    if (!fileObject.exists()) {
        fileObject.createFolder();
    } else {
        fileObject.delete(Selectors.EXCLUDE_SELF);
    }
    fileObject.copyFrom(localFileObject, Selectors.EXCLUDE_SELF);
}
Also used : DefaultFileSystemManager(org.apache.commons.vfs2.impl.DefaultFileSystemManager) SftpFileSystemConfigBuilder(com.jn.agileway.vfs.provider.sftp.SftpFileSystemConfigBuilder) FileObject(org.apache.commons.vfs2.FileObject) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions) Test(org.junit.Test)

Aggregations

SftpFileSystemConfigBuilder (com.jn.agileway.vfs.provider.sftp.SftpFileSystemConfigBuilder)1 FileObject (org.apache.commons.vfs2.FileObject)1 FileSystemOptions (org.apache.commons.vfs2.FileSystemOptions)1 DefaultFileSystemManager (org.apache.commons.vfs2.impl.DefaultFileSystemManager)1 Test (org.junit.Test)1