Cross-Platform, Java

Establish FTP connection with Java

To establish a connection with a FTP server we are going to use Apache commons Net library. You can download it from here.

The code below is licensed under GPLv3.

/*
 *  SimpleFTP connection
 *  Open Source FTP Connection Client sample
 *  Copyright (C) 2011 SimpleFTP KARPOUZAS GEORGE
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package simpleftp;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
public class SimpleFTP {
    public static void main(String[] args) {
        FTPClient ftpclient = new FTPClient();
        try {
            ftpclient.connect("[ftp_server_address]");
            if (ftpclient.login("[ftp_username]", "[ftp_user_passwd]")) {
                System.out.println("Successful login!");
                if (ftpclient.logout())
                    System.out.println("You have been logged out from FTP server.");
            } else
                System.out.println("Login failed!");
        } catch (IOException e) {
            System.err.println(e.getMessage());
        } finally {
            try {
                ftpclient.disconnect();
            } catch (IOException e) {
                System.err.println(e.getMessage());
            }
        }
    }
}