使用 FTPClient Java 检索文件内容

发布于 2024-10-09 10:51:57 字数 118 浏览 0 评论 0原文

我使用commons FTPCLIENT 我只想从 ftp 服务器获取文件内容。 我不想将其写入临时文件。 有什么办法可以做到这一点吗? fileoutputstream 应始终指向本地文件。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花ソ最美的依靠 2024-10-16 10:51:57

您应该使用retrieveFilestream 方法而不是retriveFile 方法。

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;

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;
左岸枫 2024-10-16 10:51:57

非常感谢您的快速回复..

这对我有用..
这就是我尝试过的。

-

 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文