How do you upload a file to an FTP server?

Use the FTPClient Class from the Apache Commons Net library.

This is a snippet with an example:

FTPClient client = new FTPClient();
FileInputStream fis = null;

try {
    client.connect("ftp.domain.com");
    client.login("admin", "secret");

    //
    // Create an InputStream of the file to be uploaded
    //
    String filename = "Touch.dat";
    fis = new FileInputStream(filename);

    //
    // Store file to server
    //
    client.storeFile(filename, fis);
    client.logout();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (fis != null) {
            fis.close();
        }
        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Snippet taken from http://www.kodejava.org/examples/356.html

Leave a Comment