2014-09-26 33 views
1

我需要複製並粘貼動態遞增log file data from FTP Server to local drive將動態遞增的日誌文件數據從FTP複製到本地

我使用的下面的程序只能複製一次。而不是在incremental manner

public class ReadFtpFile { 

    public static void main(String[] args) throws UnknownHostException { 

    String server = "myIP"; 
    int port = 20; 
    String user = "username"; 
    String pass = "password"; 

    FTPClient ftpClient = new FTPClient(); 
    try { 

     ftpClient.connect(server, port); 
     ftpClient.login(user, pass); 
     ftpClient.enterLocalPassiveMode(); 
     ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 

     // APPROACH #2: using InputStream retrieveFileStream(String) 
     String remoteFile2 = "/folder/myfile.log"; 
     File downloadFile2 = new File("F:/myfolder/mylogfile.log"); 
     OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2)); 
     InputStream inputStream = ftpClient.retrieveFileStream(remoteFile2); 
     byte[] bytesArray = new byte[4096]; 
     int bytesRead = -1; 
     while ((bytesRead = inputStream.read(bytesArray)) != -1) { 
     outputStream2.write(bytesArray, 0, bytesRead); 
     } 

     Boolean success = ftpClient.completePendingCommand(); 
     if (success) { 
     System.out.println("File #2 has been downloaded successfully."); 
     } 
     outputStream2.close(); 
     inputStream.close(); 

    } catch (IOException ex) { 
     System.out.println("Error: " + ex.getMessage()); 
     ex.printStackTrace(); 
    } finally { 
     try { 
     if (ftpClient.isConnected()) { 
      ftpClient.logout(); 
      ftpClient.disconnect(); 
     } 
     } catch (IOException ex) { 
     ex.printStackTrace(); 
     } 
    } 
    } 

} 

FTP服務器中的日誌文件數據增長速度爲每second。我需要更新的FTP新數據的本地文件。

+0

你不能配置你的FTP服務器來滾動日誌文件嗎? – walkeros 2014-10-01 05:52:22

回答

3

更換線

OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2)); 
InputStream inputStream = ftpClient.retrieveFileStream(remoteFile2); 

ftpClient.setRestartOffset(downloadFile2.length()); 
InputStream inputStream = ftpClient.retrieveFileStream(remoteFile2); 
OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2, true)); 

這會檢查文件是否已經存在,如果是這樣,只下載新的數據。如果您需要定期執行此操作,請在整個try-catch塊中添加一個循環。

0

您需要使用Java線程更新代碼,並結合while循環來安排此程序的所需時間。

String remoteFile2 = "/folder/myfile.log"; 

    File downloadFile2 = new File("F:/myfolder/mylogfile.log"); 

    OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2)); 

    InputStream inputStream = ftpClient.retrieveFileStream(remoteFile2); 

    byte[] bytesArray = new byte[4096]; 
    int bytesRead = -1; 
    int minutecount=0; 

    while(minutecount==120){ 
    while ((bytesRead = inputStream.read(bytesArray)) != -1) { 
     outputStream2.write(bytesArray, 0, bytesRead); 
     } 
    // Here i sceduled for every 1 minute 
     Thread.sleep(60*1000); 
     minutecount++; 
    } 
    Boolean success = ftpClient.completePendingCommand(); 
    if (success) { 
    System.out.println("File #2 has been downloaded successfully."); 
    } 
    outputStream2.close(); 
    inputStream.close();` 
相關問題