Problem with FTPClient class in java

10,161

Solution 1

According to the API for FTPClient.retrieveFileStream(), the method returns null when it cannot open the data connection, in which case you should check the reply code (e.g. getReplyCode(), getReplyString(), getReplyStrings()) to see why it failed. Also, you are suppose to finalize file transfers by calling completePendingCommand() and verifying that the transfer was indeed successful.

Solution 2

It works ok when I add after the "retrieve" command :

        int response = client.getReply();
        if (response != FTPReply.CLOSING_DATA_CONNECTION){
            //TODO 
        }
Share:
10,161
Yevgeny Simkin
Author by

Yevgeny Simkin

I fled Soviet Russia as a child and have spent my life bouncing from music to comedy to software engineering. You can follow my comedy Twitter feed here. I'm also the founder and CEO of The Russian Mob™—an agency specializing in developing SAAS, Mobile, AR, VR, and Web applications. (No: we won’t help you hack a foreign election so don’t bother asking.) On occasion, things that I think are published over at The Bulwark, which you should be reading, even if my ideas weren't being published there. If you would like to connect with me, the easiest way is through LinkedIn.

Updated on June 04, 2022

Comments

  • Yevgeny Simkin
    Yevgeny Simkin almost 2 years

    I'm using org.apache.commons.net.ftp.FTPClient and seeing behavior that is, well... perplexing.

    The method beneath intends to go through an FTPFile list, read them in and then do something with the contents. That's all working. What is not (really) working is that the FTPClient object does the following...

    1) Properly retrieves and stores the FIRST file in the list  
    2) List item evaluates to NULL for x number of successive iterations of the loop (x varies on successive attempts  
    3) manages to retrieve exactly 1 more file in the list  
    4) reports that it is null for exactly 1 more file in the list  
    5) hangs indefinitely, reporting no further activity.
    
    public static String mergeXMLFiles(List<FTPFile> files, String rootElementNodeName, FTPClient ftp){
            String ret = null;
            String fileAsString   = null; 
            //InputStream inStream;
            int c;
    
            if(files == null || rootElementNodeName == null)
                return null;
            try {
                System.out.println("GETTING " + files.size() + " files");
                for (FTPFile file : files) {
                    fileAsString = "";
                    InputStream inStream = ftp.retrieveFileStream(file.getName());
    
                    if(inStream == null){
                        System.out.println("FtpUtil.mergeXMLFiles() couldn't initialize inStream for file:" + file.getName());
    
                        continue;//THIS IS THE PART THAT I SEE FOR files [1 - arbitrary number (usually around 20)] and then 1 more time for [x + 2] after [x + 1] passes successfully.
                    }
                    while((c = inStream.read()) != -1){
    
                        fileAsString += Character.valueOf((char)c);
                    }
                    inStream.close();
    
    
                    System.out.println("FILE:" + file.getName() + "\n" + fileAsString);
                }
    
    
            } catch (Exception e) {
                System.out.println("FtpUtil.mergeXMLFiles() failed:" + e);
            }
            return ret;
        }
    

    has anyone seen anything like this? I'm new to FTPClient, am I doing something wrong with it?

  • Yevgeny Simkin
    Yevgeny Simkin about 15 years
    completePendingComand()!!!!! You are a gentleman AND a scholar I was checking the replyCode (in the preceding method where I do all the connection junk) and that was all working fine. I overlooked the documentation that specified the importance of completePendingCommand. Thanks very much.
  • krishnang
    krishnang over 10 years
    Thanks for this answer Zach Scrivena! Was stuck with this issue for a long time :)