Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ftpClient.retrieveFileStream(filepath) returning null

Tags:

java

ftp

I have a folder and it has 3 files in ftp.For first file the bufferreader is reading successfully.But from second file InputStream is getting as null.Dont know what is the reason.

My code is

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 i am getting InputStream as null from the second file??

like image 861
Srinath Murugula Avatar asked May 18 '16 10:05

Srinath Murugula


1 Answers

I faced the same issue and finalising the file transfers by calling completePendingCommand() and verifying that the transfer was indeed successful fixed the issue.

As stated in the documentation

To finalize the file transfer you must call completePendingCommand and check its return value to verify success. If this is not done, subsequent commands may behave unexpectedly.

If this doesn't fix the problem use getReplyCode() or getReplyString() or getReplyStrings() to see what is the exact problem.

Edit : Just noticed that this is a pretty old post. But hope it will be helpful for someone else.

like image 144
aladeen Avatar answered Oct 12 '22 21:10

aladeen