Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Daniel Savarese
 
Language: Java, Web
Expertise: Intermediate
Aug 31, 1998

FTP with Java

Normally, from within a Java applet you can create a java.net.URL instance using the "ftp://" protocol handler. But you may need a third-party library to access FTP from a Java application. There is a package called com.sun.net.ftp that you can use, but it is non-standard and not guaranteed to be available on every platform. By using a third-party package, you can ensure your program will work everywhere by distributing the classes with your program. One package that provides FTP functions, as well as others, is called NetComponents, available from http://www.oroinc.com. It contains a package called com.oroinc.net.ftp and a class called FTPClient with which you can access ftp from a program. I've included a simple example of how to fetch a file from an FTP server and write it to the local file system:
 
import java.io.*;import
com.oroinc.net.ftp.*;public final class
FTP {  public static final void
main(String[] args) {    FTPClient
client;    client = new FTPClient();
try {      client.connect("ftp.javasoft.com");
if(!FTPReply.isPositiveCompletion(
client.getReplyCode())) {
client.disconnect();
System.err.println("FTP server refused
connection.");   return;      }
client.login("anonymous",
"user@hostname");
client.setFileType(com.oroinc.net.ftp.FTP.BINARY_FILE_TYPE);
client.retrieveFile("docs/
source_license.pdf",           new
FileOutputStream("source_license.pdf"));
client.quit();
client.disconnect();    }
catch(IOException e) {
e.printStackTrace();    }  }}
Submit a Tip Browse "Application Design" Tips Browse All Tips
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date