【问题标题】:How to compare FTPClient File's last modification with a local file?如何将 FTPClient 文件的最后修改与本地文件进行比较?
【发布时间】:2015-06-08 03:31:50
【问题描述】:

我有两个文件,一个在服务器,一个在本地,我想获取两个文件的最后修改,看看哪个更新。我做了一个方法,但我总是得到相同的结果。我在每一侧都尝试了测试,但 if 语句总是做出相同的决定。

这是我的代码:

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 被修改,但总是晚一小时。是关于时区的任何事情吗?

或者有什么想法可以比较一个服务器文件与本地文件的最后修改时间?

【问题讨论】:

  • 你总是得到相同的修改时间吗?有什么问题?
  • @Paner 如果我修改文件,修改时间不会改变,但问题是服务器文件总是落后一小时。所以如果我现在修改它,它会给出一个小时前的时间。所以它总是比本地时间的文件旧。这就是问题所在。

标签: java file-io apache-commons ftp-client last-modified


【解决方案1】:

知道ftp服务器时区没有标准的方法,但是你可以做的是上传一个文件,然后计算FTP报告的文件时间和本地的时间差。这必须是作为程序的第一步运行的方法,用于在您的每个客户端应用程序中初始化时区逻辑。

【讨论】:

  • 谢谢。经过研究,我也有了同样的想法。
猜你喜欢
  • 1970-01-01
  • 2019-08-02
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-14
相关资源
最近更新 更多