Monday 11 April 2011

FTP handling using JAVA


See the following example :-

FTPOperations.java

package test;

import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPFile;
import java.io.File;

/**
 *
 * @author 05705
 */
public class FTPOperations {

    private FTPClient client = null;
    private String ftpFolder = "prasobh/myfolder";
    private String server = "192.168.39.55";
    private String user = "prasobh";
    private String password = "password";
    private String tempFolder = "C://myLocalFolder";

    public FTPOperations() {
        //download all files in specified ftp folder
        downloadFilesFromFtp();

        //upload file to ftp location
         uploadFile() ;

    }

    public static void main(String[] args) {
    }

    public void downloadFilesFromFtp() {



        try {
            // Get the FTP Connection from the Utility class
            client = FTPUtil.connect(server, user, password);
            if (client != null) {
                // try {
                client.changeDirectory(ftpFolder);
                FTPFile[] fileArray = client.list();
                if (fileArray.length <= 1) {
                    tracer.warn("No file found at  FTP  Location : " + server + File.separator + ftpFolder + "]");
                } else {
                    for (int i = 0; i < fileArray.length; i++) {
                        FTPFile file = fileArray[i];
                        File fileDownload = new File(tempFolder + File.separator + file.getName());
                        client.download(file.getName(), fileDownload);
                    }
                }

            } else {
                tracer.warn("********  Failed to connect to FTP location server [" + server + "]");
            }
        } catch (Exception e) {
            tracer.error("ERROR : Error in Connecting and downloading file server [" + server + "] ftpFolder [" + ftpFolder + "]", e);

        } finally {
            if (client != null) {
                try {
                    client.disconnect(true);
                } catch (Exception e) {
                    tracer.error("ERROR : Error in disconnecting the Remote Machine");
                }
            }
        }
    }

    public void uploadFile() {

        String MyFile = "C:mytext.txt";
        String ftpFolder="prasobh/myuploads";
        File f = new File(MyFile);
        FTPClient client = null;
        try {
            // Get the FTP Connection from the Utility class
            client = FTPUtil.connect(server, user, password);
            if (client != null) {
                try {
                    client.changeDirectory(ftpFolder);
                    client.upload(MyFile);
                } catch (Exception e) {
                    tracer.error("ERROR : Error copying file[" + MyFile + "] to Remote Machine", e);
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            tracer.error("ERROR : Error in Connecting to Remote Machine");
        } finally {
            if (client != null) {
                try {
                    client.disconnect(true);
                } catch (Exception e) {
                    tracer.error("ERROR : Error in disconnecting the Remote Machine");
                }
            }
        }
    }
}

FTPUtil.java


import it.sauronsoftware.ftp4j.FTPClient;

public class FTPUtil
{
        public static FTPClient connect(String ipAddress, String userName, String password)
        {
                FTPClient client = new FTPClient();            
                try
                {
                        client.setType(FTPClient.TYPE_BINARY);
                        client.connect(ipAddress);
                        client.login(userName, password);
                        return client;
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                        return null;
                }
        }
}

 use following jar

1. ftp4j.jar




No comments:

Post a Comment