How to specify directory / file files using Apache Common vfs

I'm new to Apache Common vfs, Success connecting to the server I already read the docs, but I'm stuck in this code. How can I list the directory / files?

.... Session session = null; FileSystemManager fsManager = null; FileSystem fs = null; try { String host = "host_here"; int port = 22; String userStr = "user_here"; char [] username = userStr.toCharArray(); String passStr = "password_here"; char [] password = passStr.toCharArray(); session = SftpClientFactory.createConnection(host, port, username, password, null); //session.connect(); System.out.println("Connected to the server"); FileSystemOptions opts = new FileSystemOptions(); fsManager = VFS.getManager(); FileObject file = fsManager.resolveFile("ftp://"+userStr+":"+passStr+"@"+host+"/home/", opts); // .... whats next i do here? ..... } catch (Exception e) { session.disconnect(); e.printStackTrace(); } ... 

Please help me, thank you earlier :)

+7
source share
1 answer

A list of files can be displayed using the FileObject # getChildren () method .

 FileSystemOptions opts = new FileSystemOptions(); fsManager = VFS.getManager(); // List all the files in that directory.Try to give the directory path FileObject localFileObject=fsManager.resolveFile("ftp://"+userStr+":"+passStr+"@"+host+"/home"); FileObject[] children = localFileObject.getChildren(); for ( int i = 0; i < children.length; i++ ){ System.out.println( children[ i ].getName().getBaseName() ); } // End of List Files. FileObject file = fsManager.resolveFile("ftp://"+userStr+":"+passStr+"@"+host+"/home/", opts); 

My suggestion would be to use JSCH , which is best for SFTP operations. Since this Apache Common VFS inherently used this structure. Difficulty will be greatly reduced by JSCH .

+6
source

All Articles