public static String loadFileFromFTPServer ( String hostName , Integer port , String userName , String password , String filePath , int numberOfLines ) { String result = null ; FTPClient ftpClient = new FTPClient ( ) ; InputStream inputStream = null ; String errorMessage = "Unable to connect and download file '%s' from FTP server '%s'." ; try { connectAndLoginOnFTPServer ( ftpClient , hostName , port , userName , password ) ; // load file into string ftpClient . enterLocalPassiveMode ( ) ; inputStream = ftpClient . retrieveFileStream ( filePath ) ; validatResponse ( ftpClient ) ; result = FileUtil . streamToString ( inputStream , filePath , numberOfLines ) ; ftpClient . completePendingCommand ( ) ; } catch ( IOException ex ) { throw new RuntimeException ( String . format ( errorMessage , filePath , hostName ) , ex ) ; } finally { closeInputStream ( inputStream ) ; disconnectAndLogoutFromFTPServer ( ftpClient , hostName ) ; } return result ; }
Reads content of file on from FTP server to String .