Retrieving file content using FTPClient Java

2019-06-17 01:01发布

Im using commons FTPCLIENT I just want the file content from the ftp server. i dont want to write it to a temporary file. Is there any way to do that. The fileoutputstream should always point to a local file.

Thanks in advance.

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-17 01:36

Thanks a lot for the quick reply..

And that did work 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
查看更多
等我变得足够好
3楼-- · 2019-06-17 01:41

You should use retrieveFilestream method instead of 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;
查看更多
登录 后发表回答