2010-12-27 44 views
6

Im sử dụng commons FTPCLIENT Tôi chỉ muốn nội dung tệp từ máy chủ ftp. tôi không muốn ghi nó vào một tập tin tạm thời. Có cách nào để làm điều đó không. Fileoutputstream phải luôn trỏ đến một tệp cục bộ.Lấy nội dung tệp bằng FTPClient Java

Xin cảm ơn trước.

+0

Kiểm tra [FTPClientDemonstration] (http://www.java2s.com/Code/Java/Network-Protocol/Ftpclientdemonstration.htm) –

Trả lời

3

Bạn nên sử dụng phương pháp retrieveFilestream thay vì phương pháp 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; 
0

Cảm ơn rất nhiều bạn đã trả lời nhanh chóng ..

Và đó đã làm việc cho tôi .. đây là những gì tôi đã cố gắng.

-

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 
Các vấn đề liên quan