在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("http://www.yiibai.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("http://www.yiibai.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