Java FTP Tips

The simplest way to access an FTP site is to use a URL string. You may be able to do something like this:

URL url =
new URL("ftp://username:password@ftp.whatever.com/file.zip;type=i");
URLConnection con = url.openConnection();
BufferedInputStream in =
new BufferedInputStream(con.getInputStream());
FileOutputStream out =
new FileOutputStream("C:\\file.zip");

int i = 0;
byte[] bytesIn = new byte[1024];
while ((i = in.read(bytesIn)) >= 0) {
out.write(bytesIn, 0, i);
}
out.close();
in.close();

This method can work if:

* the site supports it
* you know exactly what file you need and where it is
* you don't have to do anything other than just download a file

No comments:

Post a Comment