2015-04-03 25 views
2

我在服務器中有兩個文件,一個是本地文件,我想獲得兩個文件的最後修改,並查看哪一個更新。我做了一個方法,但我總是得到相同的結果。我試過每一方面的測試,但if語句總是做出相同的決定。如何比較FTPClient文件的上次修改與本地文件?

這裏是我的代碼:

public void SyncCheck(FTPClient ftpClient, String remoteFilePath, String savePath) throws IOException, ParseException{ 
     String time = ftpClient.getModificationTime(remoteFilePath); 
     Date remoteFileDate = timeSplitter(time); 
     Date LocalFileDate = new Date(new File(savePath).lastModified()); 

     if(remoteFileDate.after(LocalFileDate)){ 
      System.out.println("Remote File is newer: " + remoteFileDate); 
     } 
     else{ 
      System.out.println("nothing"); 
      System.out.println("Remote " + remoteFileDate); 
      System.out.println("Local " + LocalFileDate); 
     } 
    } 

    public Date timeSplitter(String time) throws ParseException{ 
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); 
     String timePart = time.split(" ")[1]; 
     Date modificationTime = dateFormat.parse(timePart); 
     return modificationTime; 
} 

結果總是這樣:

nothing 
Remote Fri Apr 03 02:20:30 BST 2015 
Local Fri Apr 03 03:12:58 BST 2015 

無論是遠程文件更新或更舊。我注意到的另一個問題是,遠程文件在03:20:30被修改,但總是落後一個小時。是關於任何時區?

或者任何想法比較一個服務器文件的最後修改時間與本地服務器文件?

+0

你總是得到相同的修改時間嗎?問題是什麼? – ddarellis 2015-04-03 11:36:15

+0

@Paner否如果我修改文件,修改時間會發生變化,但問題是服務器文件總是落後一小時。所以如果我現在修改它,它會提供一小時前的時間。所以它總是比當地時間的文件還要舊。這就是問題所在。 – Dan 2015-04-04 22:46:39

回答

3

有沒有標準的方法來知道FTP服務器時區,但你可以做的是上傳一個文件,然後計算FTP和本地報告的文件時間之間的時間差。這必須是作爲程序的第一步運行的方法,以便在您的每個客戶機應用程序中初始化時區邏輯。

+0

謝謝。經過我的研究,我達到了同樣的想法。 – Dan 2015-04-07 03:11:44

相關問題