Example usage for java.net URLConnection getLastModified

List of usage examples for java.net URLConnection getLastModified

Introduction

In this page you can find the example usage for java.net URLConnection getLastModified.

Prototype

public long getLastModified() 

Source Link

Document

Returns the value of the last-modified header field.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL u = new URL("http://127.0.0.1/test.gif");
    URLConnection uc = u.openConnection();
    uc.setUseCaches(false);/*from w  ww. jav  a2 s  . co m*/
    long timestamp = uc.getLastModified();

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    int c;//  w w w. j  av a2s. co m
    URL hp = new URL("http", "www.java2s.com", 80, "/");
    URLConnection hpCon = hp.openConnection();
    System.out.println("Date: " + hpCon.getDate());
    System.out.println("Type: " + hpCon.getContentType());
    System.out.println("Exp: " + hpCon.getExpiration());
    System.out.println("Last M: " + hpCon.getLastModified());
    System.out.println("Length: " + hpCon.getContentLength());
}

From source file:Main.java

public static void main(String args[]) throws Exception {

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();
    System.out.println("Content-type: " + uc.getContentType());
    System.out.println("Content-encoding: " + uc.getContentEncoding());
    System.out.println("Date: " + new Date(uc.getDate()));
    System.out.println("Last modified: " + new Date(uc.getLastModified()));
    System.out.println("Expiration date: " + new Date(uc.getExpiration()));
    System.out.println("Content-length: " + uc.getContentLength());
}

From source file:Main.java

public static void main(String args[]) throws Exception {

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();
    uc.connect();// w  ww  .  j  a  va2s.  com
    System.out.println("Content-type: " + uc.getContentType());
    System.out.println("Content-encoding: " + uc.getContentEncoding());
    System.out.println("Date: " + new Date(uc.getDate()));
    System.out.println("Last modified: " + new Date(uc.getLastModified()));
    System.out.println("Expiration date: " + new Date(uc.getExpiration()));
    System.out.println("Content-length: " + uc.getContentLength());
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    int c;//from   ww  w  .j a v a2 s.  co m
    URL hp = new URL("http://www.internic.net");
    URLConnection hpCon = hp.openConnection();

    long d = hpCon.getDate();
    if (d == 0)
        System.out.println("No date information.");
    else
        System.out.println("Date: " + new Date(d));

    System.out.println("Content-Type: " + hpCon.getContentType());

    d = hpCon.getExpiration();
    if (d == 0)
        System.out.println("No expiration information.");
    else
        System.out.println("Expires: " + new Date(d));

    d = hpCon.getLastModified();
    if (d == 0)
        System.out.println("No last-modified information.");
    else
        System.out.println("Last-Modified: " + new Date(d));

    int len = hpCon.getContentLength();
    if (len == -1)
        System.out.println("Content length unavailable.");
    else
        System.out.println("Content-Length: " + len);

    if (len != 0) {
        InputStream input = hpCon.getInputStream();
        int i = len;
        while (((c = input.read()) != -1)) { // && (--i > 0)) {
            System.out.print((char) c);
        }
        input.close();

    } else {
        System.out.println("No content available.");
    }

}

From source file:radet.publisher.Test.java

public static void main(String[] args) throws Exception {
    File file = new File(System.getProperty("java.io.tmpdir"), "Opriri_programate.pdf");

    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

    Calendar ifModifiedSince = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US);
    ifModifiedSince.setTimeInMillis(file.lastModified());
    System.out.println(sdf.format(ifModifiedSince.getTime()));
    System.out.println(ifModifiedSince.getTime());

    URL url = new URL("http://radet.ro/opriri/Opriri_programate.pdf");
    URLConnection connection = url.openConnection();
    //        connection.setRequestProperty("If-Modified-Since", "Wed, 10 Aug 2016 20:35:57 GMT");

    String ifModifiedSinceString = sdf.format(ifModifiedSince.getTime());
    connection.setRequestProperty("If-Modified-Since", sdf.format(ifModifiedSince.getTime()));
    System.out.println("If-Modified-Since " + ifModifiedSinceString);

    try (InputStream urlInputStream = connection.getInputStream()) {
        try (OutputStream pdfOutputStream = new FileOutputStream(file)) {
            byte[] bytes = IOUtils.toByteArray(urlInputStream);
            System.out.println("#bytes " + bytes.length);
            IOUtils.write(bytes, pdfOutputStream);
        }//  w  w  w. j  a v  a  2 s.co  m
    }
    Calendar lastModified = Calendar.getInstance();
    lastModified.setTimeInMillis(connection.getLastModified());
    System.out.println(sdf.format(lastModified.getTime()));
    System.out.println(file.getAbsolutePath());
    System.out.println(connection.getContentLengthLong());
}

From source file:URLConnectionTest.java

public static void main(String[] args) {
    try {//from w w w.  j av a  2 s.c o  m
        String urlName;
        if (args.length > 0)
            urlName = args[0];
        else
            urlName = "http://java.sun.com";

        URL url = new URL(urlName);
        URLConnection connection = url.openConnection();

        // set username, password if specified on command line

        if (args.length > 2) {
            String username = args[1];
            String password = args[2];
            String input = username + ":" + password;
            String encoding = base64Encode(input);
            connection.setRequestProperty("Authorization", "Basic " + encoding);
        }

        connection.connect();

        // print header fields

        Map<String, List<String>> headers = connection.getHeaderFields();
        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
            String key = entry.getKey();
            for (String value : entry.getValue())
                System.out.println(key + ": " + value);
        }

        // print convenience functions

        System.out.println("----------");
        System.out.println("getContentType: " + connection.getContentType());
        System.out.println("getContentLength: " + connection.getContentLength());
        System.out.println("getContentEncoding: " + connection.getContentEncoding());
        System.out.println("getDate: " + connection.getDate());
        System.out.println("getExpiration: " + connection.getExpiration());
        System.out.println("getLastModifed: " + connection.getLastModified());
        System.out.println("----------");

        Scanner in = new Scanner(connection.getInputStream());

        // print first ten lines of contents

        for (int n = 1; in.hasNextLine() && n <= 10; n++)
            System.out.println(in.nextLine());
        if (in.hasNextLine())
            System.out.println(". . .");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.nuxeo.theme.jsf.facelets.vendor.Util.java

public static long getLastModified(URL url) {
    long lastModified;
    URLConnection conn;/*from w  w w .  j  a  v  a  2 s. com*/
    InputStream is = null;

    try {
        conn = url.openConnection();

        if (conn instanceof JarURLConnection) {
            /*
             * Note this is a work around for JarURLConnection since the getLastModified method is buggy. See
             * JAVASERVERFACES-2725 and JAVASERVERFACES-2734.
             */
            JarURLConnection jarUrlConnection = (JarURLConnection) conn;
            URL jarFileUrl = jarUrlConnection.getJarFileURL();
            URLConnection jarFileConnection = jarFileUrl.openConnection();
            lastModified = jarFileConnection.getLastModified();
            jarFileConnection.getInputStream().close();
        } else if (conn instanceof Connection) {
            // Nuxeo patch: do not bother opening connection to get last
            // modified time
            lastModified = conn.getLastModified();
        } else {
            is = conn.getInputStream();
            lastModified = conn.getLastModified();
        }
    } catch (IOException e) {
        throw new FacesException(LAST_MODIFIED_ERROR + url, e);
    } finally {
        IOUtils.closeQuietly(is);
    }
    return lastModified;
}

From source file:org.ow2.proactive.resourcemanager.updater.RMNodeUpdater.java

private static boolean isLocalJarUpToDate(String url, String filePath) {

    try {//from  w w  w  .j a v  a2s  . com
        URLConnection urlConnection = new URL(url).openConnection();
        File file = new File(filePath);

        System.out.println("Url date=" + new Date(urlConnection.getLastModified()));
        System.out.println("File date=" + new Date(file.lastModified()));

        if (file.lastModified() < urlConnection.getLastModified()) {
            System.out.println("Local jar " + file + " is obsolete");
        } else {
            System.out.println("Local jar " + file + " is up to date");
            return true;
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

    return false;
}

From source file:org.kalypso.commons.java.net.UrlUtilities.java

/**
 * Tires to find a 'lastModified' timestamp from an {@link URL}.
 *///  ww w .  ja  v  a  2  s.  c om
public static Date lastModified(final URL location) {
    if (location == null)
        return null;

    try {
        final URLConnection connection = location.openConnection();
        connection.connect();

        final long lastModified = connection.getLastModified();
        // BUGFIX: some URLConnection implementations (such as eclipse resource-protokoll)
        // do not return lastModified correctly. If we have such a case, we try some more...
        if (lastModified != 0)
            return new Date(lastModified);

        final File file = FileUtils.toFile(location);
        if (file != null)
            return new Date(file.lastModified());

        final IPath path = ResourceUtilities.findPathFromURL(location);
        if (path == null)
            return null;

        final File resourceFile = ResourceUtilities.makeFileFromPath(path);
        return new Date(resourceFile.lastModified());
    } catch (final IOException e) {
        // ignore, some resources cannot be checked at all
    }

    return null;
}