Hi

I have used ftp.jar .

and imported  com.enterprisedt.net.ftp.*;

using that i have fetched the file from client side server.

flag = retrieveFileFTP(hostName, userName, password, ftpDir, salesFileName,
args[2]);

args[2] is the command line argument i have send when running the thread.
you can send your local directory .


you need to pass the above parameters to fetch the file.

Below is the code to Fetch the file :

public boolean retrieveFileFTP(String hostName, String userName, String
password, String path, String fileName, String destDir) throws IOException,
FTPException {


        FTPClient ftp = null;
        FTPMessageCollector fmc = new FTPMessageCollector();

        try {
            String pwd = null;

            ftp = new FTPClient(hostName, 21);
            System.out.println("testing login");
            ftp.debugResponses(true);

            ftp.login(userName, password);
            System.out.println("logging again");
            fmc.logCommand("login connected ");

            ftp.setType(FTPTransferType.BINARY);
            System.out.println("transfer type set to BINARY");

            ftp.setConnectMode(FTPConnectMode.PASV);
            System.out.println("setting connnect mode to PASSIVE");
            System.out.println("changing directory to web");

            if (path != null && !path.equals("null") &&
!path.trim().equals("")) {

                ftp.chdir(path);
                pwd = ftp.pwd();
            }

            //ftp.chdir("noida/images");

            System.out.println(ftp.pwd());
            //System.out.println("File Name " + ftpDir);
            String file = new String();
            //added by vineeta::start

            String fileListing[] = ftp.dir();
            //     System.out.println("fileListing"+fileListing.length);
            String originalfileName = salesFileName;
            int found = 0;
            for (int i = 0; i < fileListing.length; i++) {
                if (fileListing[i].equalsIgnoreCase(fileName)) {

                    found = 1;

                    originalfileName = fileListing[i];
                    System.out.println("file name in original=" +
originalfileName);
                    fileName = originalfileName;
                    salesFileName = fileName;
                }
                if (found == 1) {
                    break;
                }

            }
            //added by vineeta:: end

            if (path != null && !path.equals("null") &&
!path.trim().equals("")) {

                //changed ftp.get(destDir+"/"+fileName,pwd+"/"+fileName);
                ftp.get(destDir + "/" + fileName, pwd + "/" +
originalfileName);
            } else {

                //changed ftp.get(destDir+"/"+fileName,fileName);
                ftp.get(destDir + "/" + fileName, originalfileName);
            }
            //System.out.println(ftp.get("aab.sls.csv"));
            //ftp.get("aab.sls.csv");


//ftp.put(siteSrcDirectory.getAbsolutePath(),siteSrcDirectory.getName());//storeFile(siteFiles[i],
ftpSession);

            fmc.logCommand("<##################### downloading complete for
###############>");
            System.out.println("<##################### downloading complete
for  ###############>");
        //libraryDocumentsDAO.updateStatus(hostName + "/" + ftpDir);
        } catch (Exception e) {
            e.printStackTrace();
            fmc.logCommand(" In exception block   >>>>>>>>>>>>>>>>:   " +
e);
            //logger.error("Exception:   "+e);
            try {
                MailSender mailSender = new MailSender(smtpServer);

                //String exMessage = "<br>Sales File "+fileName+" wasn't
downloaded successfully due to <br><br>Exception-->"+e ;
                String exMessage = "<br>Sales File " + fileName + " wasn't
downloaded successfully due to <br><br>Exception-->" + e + "<br> The Ftp
Server is: " + hostName + ",User name is:" + userName + ",Password is: " +
password + ",Path is :" + path;
                String[] to = {"[email protected]", "
[email protected]", "[email protected]"};

                mailSender.sendMail("Franconnect Administrator", to, "Pizza
Vito Sales File Not Downloaded", exMessage, "text/html");

            } catch (Exception exp) {
                e.printStackTrace();
                fmc.logCommand("exception >>>>>>>>>>>>>>>" + e);
            //logger.error("Exception: "+exp );
            } finally {
            }

            return false;

        } finally {
            //logger.error("Quting: ");
            //logger.error("  ");

            try {
                fmc.logCommand(" QUIT >>>>>>>>>>>>>>>>>>>>>>>>>>>   :    ");
                ftp.quit();
            } catch (Exception e) {
                fmc.logCommand("Exception >>>>>>>>>>>>>>>>>>    :  " + e);
                //    logger.error(" Final exception    :    "+e);
                e.printStackTrace();
            }
        }

        return true;



    }
}


The file will be on your local and you can read it through BufferedReader

File salesFile = new File(destDir);
            long fSz = salesFile.length();

            BufferedReader fileReader = null;
            int i = 0;
            try{

                 fileReader = new BufferedReader(new FileReader(destDir));

line will be read through

 line = fileReader.readLine())


Apply your code on reading the line.



Thanks

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to