How do I download file from FTP server?

Category: commons.net, viewed: 13894 time(s).

This example demonstrate how to download a file from FTP server.

package org.kodejava.example.commons.net;

import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
import java.io.FileOutputStream;

public class FtpDownloadDemo {
    public static void main(String[] args) {
        FTPClient client = new FTPClient();
        FileOutputStream fos = null;

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

            //
            // The remote filename to be downloaded.
            //
            String filename = "sitemap.xml";
            fos = new FileOutputStream(filename);

            //
            // Download file from FTP server
            //
            client.retrieveFile("/" + filename, fos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats