Commons FTPClient InputStream from multiple files in one connection

My requirement is to get a list of files of the required file extension from different directories, and when I receive the files I have to get the input stream and do some processing .... I try to get the list of files with the necessary file extension, then I try to get the input stream from all the selected files on this list. For the first iteration, I get the required files, and also get the stream for one file, but for the next file, I get an exception with a null pointer. I also cannot get a list of files from the second iteration

Sample code that I used for testing:

System.out.println(ftp.printWorkingDirectory()); boolean status = ftp .changeWorkingDirectory("mydirectory"); System.out.println("Status of Change Directory:" + status); System.out.println(ftp.printWorkingDirectory()); InputStream is = null; System.out.println(ftp.printWorkingDirectory()); System.out.println(ftp.isConnected()); FTPFile[] list2 = ftp.listFiles(); System.out.println("Number of files in this directory:" + list2.length); for (int i = 0; i < list2.length; i++) { System.out.println("-------[" + list2[i].getName() + "]---------------------"); is = ftp.retrieveFileStream(list2[i].getName()); int bytesRead = 0; byte[] buffer = new byte[1024]; while ((bytesRead = is.read(buffer, 0, buffer.length)) > 0) { System.out.println(new String(buffer, 0, bytesRead)); } //is.close(); System.out.println("-------[END:" + list2[i].getName() + "]---------------------"); } 

The number of files is displayed as 3, and the first file in "mydirectory" is read correctly, but when it tries to read the second file, it talks about the null pointer exception .... also after reading the stream using the retrieveFileStream method if I try to print the current working directory it gives a null value, but says it is still connected ....

Please let me know if I have some errors in my code .... The only workaround for me was to connect to the ftp location for each read input stream, which is not good to do ...

+4
source share
2 answers
  System.out.println(ftp.printWorkingDirectory()); boolean status = ftp .changeWorkingDirectory("mydirectory"); System.out.println("Status of Change Directory:" + status); System.out.println(ftp.printWorkingDirectory()); InputStream is = null; System.out.println(ftp.printWorkingDirectory()); System.out.println(ftp.isConnected()); FTPFile[] list2 = ftp.listFiles(); System.out.println("Number of files in this directory:" + list2.length); for (int i = 0; i < list2.length; i++) { System.out.println("-------[" + list2[i].getName() + "]---------------------"); is = ftp.retrieveFileStream(list2[i].getName()); int bytesRead = 0; byte[] buffer = new byte[1024]; while ((bytesRead = is.read(buffer, 0, buffer.length)) > 0) { System.out.println(new String(buffer, 0, bytesRead)); } is.close(); while(!ftpClient.completePendingCommand()); System.out.println("-------[END:" + list2[i].getName() + "]---------------------"); } 

while (! FtpClient.completePendingCommand ()); To complete the file transfer, you must call completePendingCommand () and check its return value to confirm success.

There are several FTPClient methods that do not execute the entire sequence of FTP commands to complete a transaction. These commands require some programmer action after receiving a positive intermediate command. After the programmer’s code completes its operations, it must call this method to receive a completion response from the server and verify the success of the entire transaction. This is what the community API says, http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html#retrieveFileStream(java.lang.String)

+4
source

There are two optimizations that I would suggest. First, I would recommend using FTPClient.listNames. This will return an array of strings. Secondly, use IOUtils.readFully (inputStream), which returns a string representation of the contents in the file.

Now, going to the problem, I assume that you are executing these commands in passive locl mode. include this in your code above ftp.retreiveFileStream.

 ftp.enterLocalPassiveMode(); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); ftp.setRemoteVerificationEnabled(false); 
0
source

Source: https://habr.com/ru/post/1403155/


All Articles