 |
Return to article.
Listing 3: Transferring data
/**
* Transfer a file from a remote host
* @param remoteFile Remote host file must exist
* @param localFile Whre should the remote file be stored
* @transferType FTP transfer type: (ASCII/BINARY)
*/
public void download(String remoteFile, String localFile, int transferType) {
try {
// remote file size
long size = client.getSize(remoteFile);
// check if remote file exists, if not an exeception will be thrown...
if ( client.exists(remoteFile) ) {
client.setType(transferType);
/** required to transfer multiple files **/
client.setLocalPassive();
client.setActive();
final FileOutputStream fos = new FileOutputStream(localFile);
// get file, use the DataSink interface to write incoming data
// Implement it to provide your own ways of storing data.
// It must be thread safe; in parallel transfer mode several streams may attempt to write.
client.get(remoteFile, new DataSink(){
public synchronized void write(Buffer buffer)
throws IOException
{
System.err.println("received " + buffer.getLength() + " bytes");
fos.write(buffer.getBuffer());
}
public void close() throws IOException
{
// close File output streams
fos.flush();
fos.close();
};
}, null);
// transfer ok
logger.info("Successfully transfered: " + remoteFile + " to " + localFile + " size: " + size);
}
else {
System.err.println(remoteFile + " doesn't exist");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
|
Return to article.
|  |
|