devxlogo

FTP with Java

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.*;importcom.oroinc.net.ftp.*;public final classFTP {  public static final voidmain(String[] args) {    FTPClientclient;    client = new FTPClient();try {      client.connect("ftp.javasoft.com");if(!FTPReply.isPositiveCompletion(client.getReplyCode())) {client.disconnect();System.err.println("FTP server refusedconnection.");   return;      }client.login("anonymous","user@hostname");client.setFileType(com.oroinc.net.ftp.FTP.BINARY_FILE_TYPE);client.retrieveFile("docs/source_license.pdf",           newFileOutputStream("source_license.pdf"));client.quit();client.disconnect();    }catch(IOException e) {e.printStackTrace();    }  }}

See also  How IoT Tech Will Transform Logistics
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist