Enterprise Distributed Technologies, Java FTP Client Library

l         How to connect to an FTP server

Connecting to an FTP server with edtFTPj/Free is simply a matter of:

1.    Creating an FileTransferClient object


FileTransferClient ftp = new FileTransferClient();

2.    Setting the remote host and user credentials


ftp.setRemoteHost(host);
ftp.setUserName(username);
ftp.setPassword(password);

3.    Calling the connect() method, which connects to the server and logs in.


ftp.connect();

4.    Logging out and closing the connection.


ftp.disconnect();

 

The connect() call will return when successfully connected and logged in, or throw an exception if it fails to connect or log in.

 

Note that the automatic login can be disabled using the advanced settings, which must be obtained via getAdvancedFTPSettings().


ftp.getAdvancedFTPSettings().setAutoLogin(false);

 

If autologin is disabled, manualLogin() must be be used to log into the server after connect() has been called (and prior to any remote operations being performed):


ftp.connect();
ftp.manualLogin();

l         How to upload, download and delete a file

Uploading, downloading and deletion of files is all done through simple method calls on the FileTransferClient

 

Uploading Files


Uploading of a file is done by the following method-call:


ftp.uploadFile(localFilePath, remoteFileName);

This method uploads the file specified by the first argument and saves it on the server with the name specified by the second argument.  If the file is already present on the server then it is usually overwritten, though this depends on the server configuration.

 

File appending is also supported, whereby the contents of the local file are appended to the end of the remote file.  Appending is done by passing a third parameter to the uploadFile() method:


ftp.uploadFile(localFilePath, remoteFileName, WriteMode.APPEND);

 

Downloading Files


Downloading of a file is done by the following method-call:


ftp.downloadFile(localFilePath, remoteFileName);

This method downloads the file specified by the second argument and saves it locally with the name specified by the first argument.  If the file is already present on the local storage medium then it is overwritten.

 

A remote file can also be downloaded into memory as a byte array:

 

ftp.downloadByteArray(remoteFileName);

Deleting Files


A file may be deleted by calling the deleteFile() method.

 

Notes:


(1) It is often useful to use streams to transfer data directly to and from memory. The topic How to transfer using FTP streams explains how to do this.

l         How to transfer using FTP streams

One of the advantages of integrating FTP functionality directly into a product rather than using stand-alone FTP applications is that data can be transferred directly to and from memory. This is particularly useful when transferring dynamic content needs, such as the results of database queries and other application data.

 

FileTransferClient allows users to use InputStreams and OutputStreams to read and write to FTP servers, in the same way that a file or socket can be read from or written to. This is done using FileTransferClient.downloadStream() and FileTransferClient.uploadStream() methods.

 

To upload a string:

 

string s = "Hello world";

OutputStream out = ftp.uploadStream("Hello.txt");

try {

    out.write(s.getBytes());

}

finally {

    out.close();

}

 

It is essential to close the stream before performing any other FTP operations, as this completes the transfer.

 

Similarly, to download from an FTP server, an InputStream is used. This can be read and written anywhere:

 

StringBuffer s = new StringBuffer();

InputStream in = ftp.downloadStream("Hello.txt");

try {

    int ch = 0;

    while ((ch = in.read()) >= 0) {

        s.append((char)ch);

    }

}

finally {

    in.close();

}

 

Again, it is essential to close the stream before performing any other FTP operations.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章