jFTP, Tutorial I
Chapter I: Hello FTP
Parsing command-line parameters
Connecting and logging into a server
Listing current directory
Disconnecting from server
Chapter II: FTP files
Note: Basic knowledge of using FTP and Java language are prerequisites.
In this chapter, way of connecting to server and listing of directories will be shown.
Parsing command-line parameters
Static method to parse command-line parameters is supplied.
% java -jar jftp.jar
ftp://ftp.server.com/pub
-user eternity
public static void main(String args[])
{
FtpConnect cn = FtpConnect.newConnect(args);
...
Example shown, creates instance of FtpConnect, with these values:
ftp.server.com - host to be connected;
/pub - initial directory;
eternity - username.
Rules:
- If no -user parameter is used, 'anonymous' will be used instead.
- If URL contains directory component,
this will be used as initial directory,
instead of user's home directory.
Example: ftp://ftp.server.com/pub
- If URL contains port number component,
this will be used instead of ftp default port number 21.
Example: ftp://ftp.server.com:2121/
Password needs to be set separately:
cn.setPassWord("my-sec-pass");
...
Once this is done you can reuse it to login again and again in one simple step.
Connecting and logging into a server
Ftp cl = new Ftp();
try
{ /* connect & login to host */
cl.connect(cn);
...
}
catch (IOException e)
{ System.out.println(e); }
Connecting is done in two simple steps:
- Ftp needs to be instantiated.
- It's connect method needs to be called.
Also this code can be used:
Ftp cl = new Ftp();
try
{ /* connect & login to host */
cl.connect("ftp.server.com",Ftp.PORT);
cl.login("my-user-name","my-sec-pass");
...
}
catch (IOException e)
{ System.out.println(e); }
Listing current directory
CoFile, FtpFile, LocalFile and standard java.io.File
CoFile defines basic interface for 'file objects'. It is designed to be similar to java.io.File.
It doesn't stand for an specific sort of file, it might be ordinary file on your disk, or a remote FtpFile and also another file type - for example NfsFile can be implemented.
Reason for this is code reusability:
- CoLoad can copy remote ftp file into local drive.
- But same function can also copy local file to remote host.
- Plus, local file can be copied into another local file.
- Also, ftp file from one remote host can be copied directly to another remote host.
Another benefit is that single swing component can be used once to display local files, second time to display remote ftp files.
Method pwd of Ftp object can be used to obtain current directory. This method returns a String representing current absolute pathname to directory. An instance of FtpFile is created. Note that CoFile reference is used.
/* get current directory */
CoFile dir = new FtpFile(cl.pwd(),cl);
...
FtpFile instance can be then used to list contents of directory it denotes.
/* list & print current directory */
CoFile fls[] = dir.listCoFiles();
if(fls!=null)
for(int n=0;n;n++)
System.out.println(fls[n].getName()
/* mark dirs with slash '/' char */
+(fls[n].isDirectory()?"/":""));
...
}
catch (IOException e)
{ System.out.println(e); }
Disconnecting from server
At last connection must be closed.
finally
{ /* disconnect from server
* this must be always run */
cl.disconnect(); }
Download this example FtpListTest.
Click here to read next chapter