Overview
The article will demonstrate how to use the sftp component in Secure FTP Factory to list contents of remote / local directories.
Code Example
Download SFTPListingEx.java
001 import java.io.BufferedReader;
002 import java.io.File;
003 import java.io.InputStreamReader;
004 import java.text.SimpleDateFormat;
005 import java.util.*;
006
007 import com.jscape.inet.sftp.*;
008 import com.jscape.inet.sftp.events.*;
009 import com.jscape.inet.ssh.util.SshParameters;
010
011
012 public class SFTPListingEx extends SftpAdapter
013 {
014 private String hostname;
015 private String username;
016 private String password;
017 private Sftp sftp;
018
019 public SFTPListingEx(String hostname, String username, String password)
020 {
021 // initialize local SFTP instance with hostname, username and password.
022 sftp = new Sftp(new SshParameters(hostname,
023 username,
024 password));
025
026 // register the listener so this instance can recieve events.
027 sftp.addSftpListener(this);
028 }
029
030 // open connection to the remote server.
031 public void openConnection() throws SftpException
032 {
033 sftp.connect();
034 }
035
036 // disconnect from the remote server.
037 public void closeConnection()
038 {
039 sftp.disconnect();
040 }
041
042 // set remote directory to upload to
043 public void setRemoteDir(String dir) throws SftpException
044 {
045 sftp.setDir(dir);
046 }
047
048 // set local directory to upload from
049 public void setLocalDir(String dir)
050 {
051 sftp.setLocalDir(new File(dir));
052 }
053
054 public String GetDirListingAsString() throws SftpException
055 {
056 return sftp.getDirListingAsString();
057 }
058
059 public Vector GetRemoteDirFiles() throws SftpException
060 {
061 Vector ret = new Vector();
062 Enumeration listing = sftp.getDirListing();
063 // enumerate thru listing printing filename for each entry
064 while(listing.hasMoreElements())
065 {
066 SftpFile f = (SftpFile)listing.nextElement();
067 ret.add(f.getFilename());
068 }
069 return ret;
070 }
071
072 // get remote dir listing
073 public Vector GetRemoteDirListing() throws SftpException
074 {
075 Vector ret = new Vector();
076 Enumeration listing = sftp.getDirListing();
077 // enumerate thru listing printing filename for each entry
078 while(listing.hasMoreElements())
079 {
080 ret.add(listing.nextElement());
081 }
082 return ret;
083 }
084
085 // get local dir listing
086 public Vector GetLocalDirListing() throws SftpException
087 {
088 Vector ret = new Vector();
089 // get local directory listing
090 Enumeration f = sftp.getLocalDirListing();
091 while(f.hasMoreElements())
092 {
093 ret.add(f.nextElement());
094 }
095 return ret;
096 }
097
098 // this method is invoked when when client connects to the remote server.
099 public void connected(SftpConnectedEvent evt)
100 {
101 System.out.println("Connected to host: " + evt.getHostname());
102 }
103
104 // this method is invoked when client disconnects from the remote server.
105 public void disconnected(SftpDisconnectedEvent evt)
106 {
107 System.out.println("Disconnected from host: " + evt.getHostname());
108 }
109
110 public static void main(String[] args)
111 {
112 String hostname = null;
113 String username = null;
114 String password = null;
115 try
116 {
117 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
118 System.out.print("Enter hostname : ");
119 hostname = reader.readLine();
120 System.out.print("Enter username : ");
121 username = reader.readLine();
122 System.out.print("Enter password : ");
123 password = reader.readLine();
124 reader.close();
125
126 SFTPListingEx example = new SFTPListingEx(hostname, username, password);
127 example.openConnection();
128 example.setRemoteDir("/remoteDir");
129 Vector remoteListing = example.GetRemoteDirListing();
130 example.setLocalDir("/localDir");
131 Vector localListing = example.GetLocalDirListing();
132 example.closeConnection();
133 }
134 catch(Exception e)
135 {
136 e.printStackTrace();
137 }
138
139 }
140 }
Lines 1-9 : Necessary import statements. Line 22 : Initialize sftp instance. Lines 59-70 : Get remote directory file names in a Vector. Lines 73-82 : Get remote directory listing in a Vector. Lines 86-96 : Get local directory listing in a Vector. Lines 99-108 : Events from SftpAdapter.
|
Comments