Retrieving file contents using FTPClient Java

I use commons FTPCLIENT I just want the contents of the file from the ftp server. I do not want to write it to a temporary file. Is there any way to do this. The fileoutputstream file must always point to a local file.

Thanks in advance.

+6
java apache-commons ftp
source share
3 answers
+6
source share

You should use the retrieveFilestream method instead of the retriveFile method.

 FTPClient ftp = new FTPClient(); // configuration code for ftpclient port, server etc InputStream in = ftp.getretrieveFileStream("remoteFileName"); BufferedInputStream inbf = new BufferedInputStream(in); byte buffer[] = new byte[1024]; int readCount; byte result[] = null; int length = 0; while( (readCount = inbf.read(buffer)) > 0) { int preLength = length; length += readCount; byte temp[] = new byte[result.length]; result = new byte[length]; System.arraycopy(temp,0,result,0,temp.length); System.arraycopy(buffer,0,result,preLength,readCount); } return result; 
+3
source share

Thanks so much for the quick reply.

And it worked for me .. this is what I tried.

-

  FTPclient fClient=new FTPclient(); fClient.connect("server"); Fclient.login("user","pwd"); InputStream iStream=fClient.retrieveFileStream("file"); BufferedInputStream bInf=new BufferedInputStream (iStream); int bytesRead; byte[] buffer=new byte[1024]; String fileContent=null; while((bytesRead=bInf.read(buffer))!=-1) { fileContent=new String(buffer,0,bytesRead); } enter code here 
0
source share

All Articles