 |
 |
 |
 |
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.
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.