Loading
FTP files using Apache FTP API Example

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  FTP the files using Apache FTP API


Ans)

FTP the files using Apache FTP API

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

class FtpFilesToServer {
 /**
  * Method to ftp the files to the destination server, and move the file to
  * an archive directory
  */
 protected void ftpFiles() throws ICCException
 {

  String ftpURL = "";

  String userName = "";
  String pwd ="";
  String destDir ="";

  List<File> fileList = new ArrayList(2);

  File yourFile1 = new File(yourFile1);
  File yourFile2 = new File(yourFile2);

  fileList.add(yourFile1);
  fileList.add(yourFile2);

  ftpFilesToServer(ftpURL, userName, pwd, destDir, fileList);
 }

 
    /**
     * Method to ftp files to server
     * @param String ftpURL
     * @param String userName
     * @param String pwd
     * @param String destDir - directory on remote machine, or null if root
     * @param List<File> filesToFTP - List of files to be ftp'ed
     */
    public static void ftpFilesToServer(String ftpURL, String userName, String pwd,
   String destDir, List<File> filesToFTP)
    {
     InputStream input = null;
        try
        {

            FTPClient ftp = new FTPClient();
            ftp.connect(ftpURL);

            // verify login was successful
            if (!ftp.login(userName, pwd))
            {
                ftp.logout();
            }

            ftp.enterLocalPassiveMode();

            //verify that destination directory exists
            if(destDir != null && destDir.trim().length() != 0)
            {
                if(!FTPReply.isPositiveCompletion(ftp.cwd(destDir)))
                {
                }
            }

            //transfer the files
            if(filesToFTP != null)
            {
                ListIterator li=filesToFTP.listIterator();
                while(li.hasNext())
                {
                    File fileToFTP=(File)li.next();
                    String fileName=fileToFTP.getName();

                    input = new FileInputStream(fileToFTP);
                    //ftp.storeFile(remote, local)
                    if (!ftp.storeFile(fileName, input))
                    {
                        ftp.logout();
                        throw new IOException("File failed to transfer to ftp URL [" + ftpURL + "].");
                    }
                }
            }

           ftp.logout();

        }
        catch(IOException ioex)
        {
        }finally {
            if (input != null) {
             try {
     input.close();
    } catch (IOException e) {
    }
            }
        }

    }

}



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

FTP files using Apache FTP API Example


FTP files using Apache FTP API Example

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free