 |
 |
 |
 |
How to transfer using FTP streams
|
 |
 |
 |
 |
Sometimes it is useful to be able to read from or write to the FTP server as if it
were a InputStream or OutputStream. This is possible using FTPInputStream
and FTPOutputStream.
These classes directly subclass Java's InputStream and OutputStream, and can be used in
exactly the same manner.
An instance of FTPClient or a subclass must be supplied to the stream's constructor. Currently SSHFTPClient is not supported, but will be in a future version.
An instance of FTPInputStream or FTPOutputStream can
only be used for one data transfer. It is essential that the stream is
closed when the transfer is complete, even if an exception was thrown.
Otherwise, server acknowledgements may get out of order, and the connection will need to be closed.
All
of the stream's transfer parameters are taken from the FTPClient
instance that is supplied to the constructor. For example, if the
stream is to be a binary mode transfer, binary mode must be set in the
FTPClient instance before it is supplied to the constructor.
The
following shows how an FTPInputStream can be used to download a file:
FTPInputStream in = new FTPInputStream(ftp, "myfile.zip");
BufferedOutputStream out = null;
byte[] chunk = new byte[4096];
int count = 0;
try {
out = new BufferedOutputStream(new FileOutputStream("myfile.zip"));
while ((count = in.read(chunk)) >= 0) {
out.write(chunk, 0, count);
}
}
finally {
try {
in.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
if (out != null) {
try {
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Note that care must be taken via a finally block to ensure that the FTP stream is closed (and, of course, the file stream).
FTPInputStream
can be especially useful when a large file is being downloaded and the
stream needs to be processed as it arrives. For example, if an XML file
was being downloaded, a SAX parser could parse the stream as data
arrived.