Java如何檢查檔案是否在伺服器上被修改了?

2019-10-16 22:27:57

在Java程式設計中,如何檢查檔案是否在伺服器上被修改了?

以下範例顯示如何檢查檔案是否在伺服器上進行了修改。

package com.yiibai;

import java.net.URL;
import java.net.URLConnection;

public class FileModificationTime2 {
    public static void main(String[] argv) throws Exception {
        URL u = new URL("https://www.tw511.com/static.v4/img/logo.png");
        URLConnection uc = u.openConnection();
        uc.setUseCaches(false);
        long timestamp = uc.getLastModified();
        System.out.println("The last modification time of logo.png is :" + timestamp);
    }
}

上述程式碼範例將產生以下結果 -

The last modification time of logo.png is :1481341017000

範例-2

以下是檢查檔案在伺服器上修改的範例。

package com.yiibai;


import java.net.URL;
import java.net.URLConnection;

public class FileModificationTime {
   public static void main(String[] argv)throws Exception {
      URL u = new URL("https://www.tw511.com/favicon.ico");

      URLConnection uc = u.openConnection();
      uc.setUseCaches(false);
      long timestamp = uc.getLastModified();
      System.out.println("The last modification time of file is :"+timestamp);
   }
}

上述程式碼範例將產生以下結果 -

The last modification time of file is :1367414578000