FtpClient.retrieveFileStream (file path) returning null

I have a folder and it has 3 files in ftp. For the first file, the buffer reader reads successfully. But from the second file, InputStream becomes null. I don’t know what is the reason.

My code

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.SocketException;
import java.util.logging.Logger;

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

public class ExtractFile{

 public static void main(String args[]) {

 // get an ftpClient object
 FTPClient ftpClient = new FTPClient();
 FileOutputStream fos = null;

  try {
  // pass directory path on server to connect
  ftpClient.connect("XXXX");

  // pass username and password, returned true if authentication is
 // successful
  boolean login = ftpClient.login("XXXX", "XXXXX");

 if (login) {
System.out.println("Connection established...");

FTPFile[] files=ftpClient.listDirectories();
for(int i=0;i<files.length;i++){


    FTPFile file=files[i];

    BufferedReader reader = null;
    String firstLine = null;

    try {

        String currentline=null;

        listDirectory(ftpClient, ftpClient.printWorkingDirectory(), "", 0);


    } finally {
        if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
    }



}


   boolean logout = ftpClient.logout();
   if (logout) {
    System.out.println("Connection close...");
    }
   } else {
   System.out.println("Connection fail...");
  }

  } catch (SocketException e) {
   e.printStackTrace();
  } catch (IOException e) {
  e.printStackTrace();
  } finally {
   try {
  ftpClient.disconnect();
  } catch (IOException e) {
  e.printStackTrace();
 }
}
}
 static void listDirectory(FTPClient ftpClient, String parentDir,
        String currentDir, int level) throws IOException {
    String dirToList = parentDir;
    if (!currentDir.equals("")) {
        dirToList += "/" + currentDir;
    }

    FTPFile[] subFiles = ftpClient.listFiles(dirToList);

    if (subFiles != null && subFiles.length > 0) {
        for (FTPFile aFile : subFiles) {
            String currentFileName = aFile.getName();

            if (currentFileName.equals(".")
                    || currentFileName.equals("..")) {

                continue;
            }
            for (int i = 0; i < level; i++) {
                System.out.print("\t");
            }
            if (aFile.isDirectory()) {
                System.out.println("[" + currentFileName + "]");
                listDirectory(ftpClient, dirToList, currentFileName, level + 1);
            } else {
                System.out.println(currentFileName);
                String currentline=null;
                BufferedReader reader = null;
                String firstLine = null;

                /*****here i am getting stream as null**********/
                InputStream stream = ftpClient.retrieveFileStream(dirToList+"/"+currentFileName);                   



                reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
                String[] lines=new String[5];
                int i=0;

                    //TODO 

                while((currentline=reader.readLine())!=null){
                    lines[i]=currentline;
                    i++;
                        System.out.println(".."+currentline);
                }

             }

        }
    }
  }
 }

Why am I getting an InputStream as null from the second file?

+4
source share
1 answer

I ran into the same problem and completed the file transfer by calling completePendingCommand () and confirming that the transfer was really successful, fixed the problem.

As stated in the documentation

, completePendingCommand . , .

, getReplyCode() getReplyString() getReplyStrings(), , .

: , . , - .

+4

All Articles