Example usage for java.net URLConnection getDate

List of usage examples for java.net URLConnection getDate

Introduction

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

Prototype

public long getDate() 

Source Link

Document

Returns the value of the date header field.

Usage

From source file:org.openflexo.toolbox.FileUtils.java

public static String createOrUpdateFileFromURL(URL url, File file, Map<String, String> headers) {
    long lastModified = 0;
    String fileContent = null;// w  w  w  .  j a  va2s.  co m
    if (file.exists()) {
        lastModified = file.lastModified();
        try {
            fileContent = FileUtils.fileContents(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (url != null) {
        try {
            URLConnection c = url.openConnection();
            if (headers != null) {
                for (Map.Entry<String, String> h : headers.entrySet()) {
                    c.addRequestProperty(h.getKey(), h.getValue());
                }
            }
            if (c instanceof HttpURLConnection) {
                HttpURLConnection connection = (HttpURLConnection) c;
                connection.setIfModifiedSince(lastModified);
                connection.connect();
                if (connection.getResponseCode() == 200) {
                    fileContent = FileUtils.fileContents(connection.getInputStream(), "UTF-8");
                    FileUtils.saveToFile(file, fileContent);
                }
            } else {
                if (c.getDate() == 0 || c.getDate() > lastModified) {
                    fileContent = FileUtils.fileContents(c.getInputStream(), "UTF-8");
                    FileUtils.saveToFile(file, fileContent);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return fileContent;
}

From source file:org.squale.welcom.struts.webServer.URLManager.java

/**
 * Retourn l'url a partie d'un site distant
 * //w  ww  .j a va2s  .c  o  m
 * @param pUrl url
 * @throws MalformedURLException problem sur l'url
 * @throws IOException exception
 * @return webfile
 */
private WebFile getWebFileFromHTTP(String pUrl) throws MalformedURLException, IOException {
    WebFile webFile = new WebFile(WebFile.TYPE_DISTANT);
    webFile.setUrl(new URL(pUrl));
    final URLConnection urlcon = webFile.getUrl().openConnection();
    urlcon.setUseCaches(true);
    urlcon.connect();
    webFile.setLastDate(new Date(urlcon.getLastModified()));

    if (urlcon.getLastModified() == 0) {
        webFile.setLastDate(new Date(urlcon.getDate()));
    }

    return webFile;
}