java.net.URLConnection.getLastModified()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(193)

本文整理了Java中java.net.URLConnection.getLastModified()方法的一些代码示例,展示了URLConnection.getLastModified()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URLConnection.getLastModified()方法的具体详情如下:
包路径:java.net.URLConnection
类名称:URLConnection
方法名:getLastModified

URLConnection.getLastModified介绍

[英]Returns the value of the response header field last-modified or 0 if this value is not set.
[中]返回上次修改的响应头字段的值,如果未设置此值,则返回0。

代码示例

代码示例来源:origin: org.netbeans.api/org-openide-filesystems

private java.util.Date timeFromDateHeaderField(URL url) {
  URLConnection urlConn;
  try {
    urlConn = url.openConnection();
    return new Date(urlConn.getLastModified());
  } catch (IOException ie) {
    return new java.util.Date(0);
  }
}

代码示例来源:origin: jenkinsci/jenkins

public void doIndex( StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
  URLConnection con = connect();
  // since we end up redirecting users to jnlpJars/foo.jar/, set the content disposition
  // so that browsers can download them in the right file name.
  // see http://support.microsoft.com/kb/260519 and http://www.boutell.com/newfaq/creating/forcedownload.html
  rsp.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  InputStream in = con.getInputStream();
  rsp.serveFile(req, in, con.getLastModified(), con.getContentLength(), "*.jar" );
  in.close();
}

代码示例来源:origin: plutext/docx4j

/**
 * Retrieve the last modified date/time of a URL.
 * @param url the URL
 * @return the last modified date/time
 */
public static long getLastModified(URL url) {
  try {
    URLConnection conn = url.openConnection();
    try {
      return conn.getLastModified();
    } finally {
      //An InputStream is created even if it's not accessed, but we need to close it.
      IOUtils.closeQuietly(conn.getInputStream());
    }
  } catch (IOException e) {
    // Should never happen, because URL must be local
    log.debug("IOError: " + e.getMessage());
    return 0;
  }
}

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

conn = url.openConnection();
  int hash = classpath.hashCode() ^ (int) conn.getLastModified();
  return hash;
} catch (IOException ex) {
  if (conn != null) {
    try {
      conn.getInputStream().close();
      conn.getOutputStream().close();
    } catch (IOException ex) { }

代码示例来源:origin: jenkinsci/jenkins

URLConnection uc = url.openConnection();
return uc.getLastModified();

代码示例来源:origin: jenkinsci/jenkins

long sourceTimestamp = con.getLastModified();
InputStream in = archive.getProtocol().startsWith("http") ? ProxyConfiguration.getInputStream(archive) : con.getInputStream();
CountingInputStream cis = new CountingInputStream(in);
try {

代码示例来源:origin: jooby-project/jooby

private static Supplier attr(final URL resource, final BiConsumer<Long, Long> attrs)
  throws Exception {
 if ("file".equals(resource.getProtocol())) {
  File file = new File(resource.toURI());
  if (file.exists() && file.isFile()) {
   attrs.accept(file.length(), file.lastModified());
   return () -> new FileInputStream(file);
  }
  return null;
 } else {
  URLConnection cnn = resource.openConnection();
  cnn.setUseCaches(false);
  attrs.accept(cnn.getContentLengthLong(), cnn.getLastModified());
  try {
   Closeables.closeQuietly(cnn.getInputStream());
  } catch (NullPointerException ex) {
   // dir entries throw NPE :S
   return null;
  }
  return () -> resource.openStream();
 }
}

代码示例来源:origin: xalan/xalan

/**
 * Returns the time-stamp for a document's last update
 */
private final long getLastModified(String uri) {
try {
  URL url = new URL(uri);
  URLConnection connection = url.openConnection();
  long timestamp = connection.getLastModified();
  // Check for a "file:" URI (courtesy of Brian Ewins)
  if (timestamp == 0){ // get 0 for local URI
    if ("file".equals(url.getProtocol())){
      File localfile = new File(URLDecoder.decode(url.getFile()));
      timestamp = localfile.lastModified();
    }
  }
  return(timestamp);
}
// Brutal handling of all exceptions
catch (Exception e) {
  return(System.currentTimeMillis());
}
}

代码示例来源:origin: pentaho/mondrian

public InputStream openStream() {
  try {
    final URLConnection connection = getConnection();
    this.lastModified = connection.getLastModified();
    return connection.getInputStream();
  } catch (IOException e) {
    throw Util.newInternal(
      e,
      "Error while opening properties file '" + url + "'");
  }
}

代码示例来源:origin: org.freemarker/freemarker

jarConn = jarURL.openConnection();
  return jarConn.getLastModified();
 } catch (IOException e) {
  return -1;
 } finally {
  try {
   if (jarConn != null) jarConn.getInputStream().close();
  } catch (IOException e) { }
long lastModified = conn.getLastModified();
if (lastModified == -1L && url.getProtocol().equals("file")) {

代码示例来源:origin: line/armeria

@Override
public HttpFileAttributes readAttributes() throws IOException {
  if (attrs == null) {
    final URLConnection conn = url.openConnection();
    final long length = conn.getContentLengthLong();
    final long lastModifiedMillis = conn.getLastModified();
    attrs = new HttpFileAttributes(length, lastModifiedMillis);
  }
  return attrs;
}

代码示例来源:origin: org.jenkins-ci.main/jenkins-core

public void doIndex( StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
  URLConnection con = connect();
  // since we end up redirecting users to jnlpJars/foo.jar/, set the content disposition
  // so that browsers can download them in the right file name.
  // see http://support.microsoft.com/kb/260519 and http://www.boutell.com/newfaq/creating/forcedownload.html
  rsp.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  InputStream in = con.getInputStream();
  rsp.serveFile(req, in, con.getLastModified(), con.getContentLength(), "*.jar" );
  in.close();
}

代码示例来源:origin: dropwizard/dropwizard

case "jar":
  try {
    final JarURLConnection jarConnection = (JarURLConnection) resourceURL.openConnection();
    final JarEntry entry = jarConnection.getJarEntry();
    return entry.getTime();
  URLConnection connection = null;
  try {
    connection = resourceURL.openConnection();
    return connection.getLastModified();
  } catch (IOException ignored) {
    return 0;
    if (connection != null) {
      try {
        connection.getInputStream().close();
      } catch (IOException ignored) {

代码示例来源:origin: spring-projects/spring-framework

@Override
public long lastModified() throws IOException {
  URL url = getURL();
  boolean fileCheck = false;
  if (ResourceUtils.isFileURL(url) || ResourceUtils.isJarURL(url)) {
    // Proceed with file system resolution
    fileCheck = true;
    try {
      File fileToCheck = getFileForLastModifiedCheck();
      long lastModified = fileToCheck.lastModified();
      if (lastModified > 0L || fileToCheck.exists()) {
        return lastModified;
      }
    }
    catch (FileNotFoundException ex) {
      // Defensively fall back to URL connection check instead
    }
  }
  // Try a URL connection last-modified header
  URLConnection con = url.openConnection();
  customizeConnection(con);
  long lastModified = con.getLastModified();
  if (fileCheck && lastModified == 0 && con.getContentLengthLong() <= 0) {
    throw new FileNotFoundException(getDescription() +
        " cannot be resolved in the file system for checking its last-modified timestamp");
  }
  return lastModified;
}

代码示例来源:origin: org.jvnet.hudson.main/hudson-core

public void doIndex( StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
  URLConnection con = connect();
  // since we end up redirecting users to jnlpJars/foo.jar/, set the content disposition
  // so that browsers can download them in the right file name.
  // see http://support.microsoft.com/kb/260519 and http://www.boutell.com/newfaq/creating/forcedownload.html
  rsp.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  InputStream in = con.getInputStream();
  rsp.serveFile(req, in, con.getLastModified(), con.getContentLength(), "*.jar" );
  in.close();
}

代码示例来源:origin: chewiebug/GCViewer

private FileInformation readFileInformation(URL url) {
  FileInformation fileInformation = new FileInformation();
  URLConnection urlConnection;
  try {
    if (url.getProtocol().startsWith("http")) {
      urlConnection = url.openConnection();
      ((HttpURLConnection) urlConnection).setRequestMethod("HEAD");
      try (InputStream inputStream = urlConnection.getInputStream()) {
        fileInformation.length = urlConnection.getContentLength();
        fileInformation.lastModified = urlConnection.getLastModified();
      }
    }
    else {
      if (url.getProtocol().startsWith("file")) {
        File file = new File(url.getFile());
        if (file.exists()) {
          fileInformation = new FileInformation(file);
        }
      }
    }
  }
  catch (IOException e) {
    if (LOG.isLoggable(Level.WARNING))
      LOG.log(Level.WARNING, "Failed to obtain age and length of URL " + url, e);
  }
  return fileInformation;
}

代码示例来源:origin: perwendel/spark

@Override
public long lastModified() throws IOException {
  URL url = getURL();
  if (ResourceUtils.isFileURL(url) || ResourceUtils.isJarURL(url)) {
    // Proceed with file system resolution...
    return super.lastModified();
  } else {
    // Try a URL connection last-modified header...
    URLConnection con = url.openConnection();
    customizeConnection(con);
    return con.getLastModified();
  }
}

代码示例来源:origin: org.eclipse.hudson/hudson-core

public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
  URLConnection con = connect();
  // since we end up redirecting users to jnlpJars/foo.jar/, set the content disposition
  // so that browsers can download them in the right file name.
  // see http://support.microsoft.com/kb/260519 and http://www.boutell.com/newfaq/creating/forcedownload.html
  rsp.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  InputStream in = con.getInputStream();
  rsp.serveFile(req, in, con.getLastModified(), con.getContentLength(), "*.jar");
  in.close();
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
   * returns true if the source in URL is newer than the class
   * NOTE: copied from GroovyClassLoader
   */
  private static boolean isSourceNewer(URL source, ClassNode cls) {
    try {
      long lastMod;

      // Special handling for file:// protocol, as getLastModified() often reports
      // incorrect results (-1)
      if (source.getProtocol().equals("file")) {
        // Coerce the file URL to a File
        String path = source.getPath().replace('/', File.separatorChar).replace('|', ':');
        File file = new File(path);
        lastMod = file.lastModified();
      } else {
        URLConnection conn = source.openConnection();
        lastMod = conn.getLastModified();
        conn.getInputStream().close();
      }
      return lastMod > getTimeStamp(cls);
    } catch (IOException e) {
      // if the stream can't be opened, let's keep the old reference
      return false;
    }
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Override
public long lastModified() throws IOException {
  URL url = getURL();
  if (ResourceUtils.isFileURL(url) || ResourceUtils.isJarURL(url)) {
    // Proceed with file system resolution...
    return getFile().lastModified();
  }
  // Try a URL connection last-modified header...
  URLConnection con = url.openConnection();
  useCachesIfNecessary(con);
  if (con instanceof HttpURLConnection) {
    ((HttpURLConnection) con).setRequestMethod("HEAD");
  }
  return con.getLastModified();
}

相关文章

URLConnection类方法