CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2008
    Posts
    966

    Real FTP problem...Promise

    Ok ok, a real problem here that is not RTFM. I am working with some existing code that is using commons-net FTPClient and FTPFile in order to connect to a remote server via FTP and grab the directory listing, and eventually the file.

    Now, before I get the "..why don't you just use.." stuff, I can't just upgrade anything. There are 25 applications currently running off of the same framework, so if I change one, it could/will affect other apps. Just keep that in mind if you have a suggestion for a better package please.

    The code is as such:

    Code:
     
    
    FTPClient l_ftp =
    new FTPClient(); long l_answer = 0; try { if(m_port != null && !m_port.equals("")) { l_ftp.connect(m_hostName, Integer.parseInt(m_port)); } else { l_ftp.connect(m_hostName); } if (!FTPReply.isPositiveCompletion(l_ftp.getReplyCode())) { System.out.println("ERROR: FTP server refused connection. [" + m_hostName + "]"); } else { if (!l_ftp.login(m_userName, m_password)) { System.out.println("ERROR: FTP login failed. [" + m_hostName + "]"); } else { try { if(l_ftp.isConnected()) System.out.println("Connected."); System.out.println("Parser type: " + m_parserType); if(m_remoteDir != null) l_ftp.changeWorkingDirectory(m_remoteDir); l_ftp.enterLocalPassiveMode(); if(l_ftp.enterRemotePassiveMode()) System.out.println("PASSIVE MODE"); FTPFile[] l_file = null; if(m_parserType.equals("FOO")) { //just for test/control l_file = l_ftp.listFiles(); } else { l_file = l_ftp.listFiles(m_fileListParser); //I know depricated } //However, for other apps the parser still works l_ftp.logout(); l_ftp.disconnect(); System.out.println("Disconnected."); System.out.println("Number of files: " + l_file.length); // This returns 0 every time FTPFile l_youngFile = getYoungest(l_file); ... //code after here doesn't matter since l_file is empty } catch (NullPointerException eNull) { System.out.println("Failed to check the time stamp. Probably beacuse no targets met the regex. [" + m_hostName + "]"); } ... //other catch blocks, irrevelent, no exceptions being thrown }
    The getYoungest(l_file) method returns the method with the earliest date time stamp. Basically what I am trying to do is poll the server every so often, if a new file (since the last time I ran) has been placed in the directory, go get it.

    If I use the com.enterprisedt.net.ftp.FTPConnectMode package / classes I am able to see the file listings, but I can't use those packages because they are not compatable with the commons-net and I would break a lot of existing code.

    I am able to see the files using FileZilla, I am able to see them using the free enterprisedt.net package, but I can't get the file listing using commons-net. Any ideas / suggestions?

    It was suggested to me that maybe it was not running in passive mode, I have tried to force it to be in passive mode by calling the methods that are supposed to, according to the api, set the ftp connection in passive mode. This setting is needed for the enterpriseft.net but when applied to the commons-net it still behaives the same.

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Real FTP problem...Promise

    After much research and testing I have found a solution that I would like to share, just in case anybody else has a similar problem.


    The problem was that I needed this 1 line of code:
    l_ftp.enterLocalPassiveMode();

    Only set the client side to passive and not the server and it works. I don't know much about ftp, but for some reason the client side needed to be set to passive mode, maybe somebody out there could elaborate a bit.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured