Java HTTP Response getResourceModifiedTime(URL url)

Here you can find the source of getResourceModifiedTime(URL url)

Description

Retrieves the last modified time of a provided URL.

License

Apache License

Return

a positive value indicating milliseconds since the epoch (00:00:00 Jan 1, 1970), or 0L on failure, such as a SecurityException or IOException.

Declaration

public static long getResourceModifiedTime(URL url) 

Method Source Code

//package com.java2s;
/*/*w  w  w. j a v  a2 s .c o m*/
 * Copyright 2008 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

import java.io.File;

import java.io.IOException;

import java.net.JarURLConnection;

import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    private static final String FILE_PROTOCOL = "file";
    private static final String JAR_PROTOCOL = "jar";

    /**
     * Retrieves the last modified time of a provided URL.
     * 
     * @return a positive value indicating milliseconds since the epoch (00:00:00 Jan 1, 1970), or 0L
     *         on failure, such as a SecurityException or IOException.
     */
    public static long getResourceModifiedTime(URL url) {
        long lastModified = 0L;
        try {
            if (url.getProtocol().equals(JAR_PROTOCOL)) {
                /*
                 * If this resource is contained inside a jar file, such as can happen if it's bundled in a
                 * 3rd-party library, we use the jar file itself to test whether it's up to date. We don't
                 * want to call JarURLConnection.getLastModified(), as this is much slower than using the
                 * jar File resource directly.
                 */
                JarURLConnection jarConn = (JarURLConnection) url.openConnection();
                url = jarConn.getJarFileURL();
            }
            if (url.getProtocol().equals(FILE_PROTOCOL)) {
                /*
                 * Need to handle possibly wonky syntax in a file URL resource. Modeled after suggestion in
                 * this blog entry: http://weblogs.java.net/blog/2007
                 * /04/25/how-convert-javaneturl-javaiofile
                 */
                File file;
                try {
                    file = new File(url.toURI());
                } catch (URISyntaxException uriEx) {
                    file = new File(url.getPath());
                }
                lastModified = file.lastModified();
            }
        } catch (IOException ignored) {
        } catch (RuntimeException ignored) {
        }
        return lastModified;
    }
}

Related

  1. getResourceLastModified(URLConnection connection)
  2. getResponse(HttpURLConnection httpConn, StringBuilder responseContent, StringBuilder responseCode, HashMap headers)
  3. getResponse(URL request)
  4. getResponse(URL url)
  5. getResponseAsString(HttpURLConnection conn)