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

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

本文整理了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

  1. private java.util.Date timeFromDateHeaderField(URL url) {
  2. URLConnection urlConn;
  3. try {
  4. urlConn = url.openConnection();
  5. return new Date(urlConn.getLastModified());
  6. } catch (IOException ie) {
  7. return new java.util.Date(0);
  8. }
  9. }

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

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

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

  1. /**
  2. * Retrieve the last modified date/time of a URL.
  3. * @param url the URL
  4. * @return the last modified date/time
  5. */
  6. public static long getLastModified(URL url) {
  7. try {
  8. URLConnection conn = url.openConnection();
  9. try {
  10. return conn.getLastModified();
  11. } finally {
  12. //An InputStream is created even if it's not accessed, but we need to close it.
  13. IOUtils.closeQuietly(conn.getInputStream());
  14. }
  15. } catch (IOException e) {
  16. // Should never happen, because URL must be local
  17. log.debug("IOError: " + e.getMessage());
  18. return 0;
  19. }
  20. }

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

  1. conn = url.openConnection();
  2. int hash = classpath.hashCode() ^ (int) conn.getLastModified();
  3. return hash;
  4. } catch (IOException ex) {
  5. if (conn != null) {
  6. try {
  7. conn.getInputStream().close();
  8. conn.getOutputStream().close();
  9. } catch (IOException ex) { }

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

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

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

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

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

  1. private static Supplier attr(final URL resource, final BiConsumer<Long, Long> attrs)
  2. throws Exception {
  3. if ("file".equals(resource.getProtocol())) {
  4. File file = new File(resource.toURI());
  5. if (file.exists() && file.isFile()) {
  6. attrs.accept(file.length(), file.lastModified());
  7. return () -> new FileInputStream(file);
  8. }
  9. return null;
  10. } else {
  11. URLConnection cnn = resource.openConnection();
  12. cnn.setUseCaches(false);
  13. attrs.accept(cnn.getContentLengthLong(), cnn.getLastModified());
  14. try {
  15. Closeables.closeQuietly(cnn.getInputStream());
  16. } catch (NullPointerException ex) {
  17. // dir entries throw NPE :S
  18. return null;
  19. }
  20. return () -> resource.openStream();
  21. }
  22. }

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

  1. /**
  2. * Returns the time-stamp for a document's last update
  3. */
  4. private final long getLastModified(String uri) {
  5. try {
  6. URL url = new URL(uri);
  7. URLConnection connection = url.openConnection();
  8. long timestamp = connection.getLastModified();
  9. // Check for a "file:" URI (courtesy of Brian Ewins)
  10. if (timestamp == 0){ // get 0 for local URI
  11. if ("file".equals(url.getProtocol())){
  12. File localfile = new File(URLDecoder.decode(url.getFile()));
  13. timestamp = localfile.lastModified();
  14. }
  15. }
  16. return(timestamp);
  17. }
  18. // Brutal handling of all exceptions
  19. catch (Exception e) {
  20. return(System.currentTimeMillis());
  21. }
  22. }

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

  1. public InputStream openStream() {
  2. try {
  3. final URLConnection connection = getConnection();
  4. this.lastModified = connection.getLastModified();
  5. return connection.getInputStream();
  6. } catch (IOException e) {
  7. throw Util.newInternal(
  8. e,
  9. "Error while opening properties file '" + url + "'");
  10. }
  11. }

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

  1. jarConn = jarURL.openConnection();
  2. return jarConn.getLastModified();
  3. } catch (IOException e) {
  4. return -1;
  5. } finally {
  6. try {
  7. if (jarConn != null) jarConn.getInputStream().close();
  8. } catch (IOException e) { }
  9. long lastModified = conn.getLastModified();
  10. if (lastModified == -1L && url.getProtocol().equals("file")) {

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

  1. @Override
  2. public HttpFileAttributes readAttributes() throws IOException {
  3. if (attrs == null) {
  4. final URLConnection conn = url.openConnection();
  5. final long length = conn.getContentLengthLong();
  6. final long lastModifiedMillis = conn.getLastModified();
  7. attrs = new HttpFileAttributes(length, lastModifiedMillis);
  8. }
  9. return attrs;
  10. }

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

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

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

  1. case "jar":
  2. try {
  3. final JarURLConnection jarConnection = (JarURLConnection) resourceURL.openConnection();
  4. final JarEntry entry = jarConnection.getJarEntry();
  5. return entry.getTime();
  6. URLConnection connection = null;
  7. try {
  8. connection = resourceURL.openConnection();
  9. return connection.getLastModified();
  10. } catch (IOException ignored) {
  11. return 0;
  12. if (connection != null) {
  13. try {
  14. connection.getInputStream().close();
  15. } catch (IOException ignored) {

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

  1. @Override
  2. public long lastModified() throws IOException {
  3. URL url = getURL();
  4. boolean fileCheck = false;
  5. if (ResourceUtils.isFileURL(url) || ResourceUtils.isJarURL(url)) {
  6. // Proceed with file system resolution
  7. fileCheck = true;
  8. try {
  9. File fileToCheck = getFileForLastModifiedCheck();
  10. long lastModified = fileToCheck.lastModified();
  11. if (lastModified > 0L || fileToCheck.exists()) {
  12. return lastModified;
  13. }
  14. }
  15. catch (FileNotFoundException ex) {
  16. // Defensively fall back to URL connection check instead
  17. }
  18. }
  19. // Try a URL connection last-modified header
  20. URLConnection con = url.openConnection();
  21. customizeConnection(con);
  22. long lastModified = con.getLastModified();
  23. if (fileCheck && lastModified == 0 && con.getContentLengthLong() <= 0) {
  24. throw new FileNotFoundException(getDescription() +
  25. " cannot be resolved in the file system for checking its last-modified timestamp");
  26. }
  27. return lastModified;
  28. }

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

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

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

  1. private FileInformation readFileInformation(URL url) {
  2. FileInformation fileInformation = new FileInformation();
  3. URLConnection urlConnection;
  4. try {
  5. if (url.getProtocol().startsWith("http")) {
  6. urlConnection = url.openConnection();
  7. ((HttpURLConnection) urlConnection).setRequestMethod("HEAD");
  8. try (InputStream inputStream = urlConnection.getInputStream()) {
  9. fileInformation.length = urlConnection.getContentLength();
  10. fileInformation.lastModified = urlConnection.getLastModified();
  11. }
  12. }
  13. else {
  14. if (url.getProtocol().startsWith("file")) {
  15. File file = new File(url.getFile());
  16. if (file.exists()) {
  17. fileInformation = new FileInformation(file);
  18. }
  19. }
  20. }
  21. }
  22. catch (IOException e) {
  23. if (LOG.isLoggable(Level.WARNING))
  24. LOG.log(Level.WARNING, "Failed to obtain age and length of URL " + url, e);
  25. }
  26. return fileInformation;
  27. }

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

  1. @Override
  2. public long lastModified() throws IOException {
  3. URL url = getURL();
  4. if (ResourceUtils.isFileURL(url) || ResourceUtils.isJarURL(url)) {
  5. // Proceed with file system resolution...
  6. return super.lastModified();
  7. } else {
  8. // Try a URL connection last-modified header...
  9. URLConnection con = url.openConnection();
  10. customizeConnection(con);
  11. return con.getLastModified();
  12. }
  13. }

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

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

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

  1. /**
  2. * returns true if the source in URL is newer than the class
  3. * NOTE: copied from GroovyClassLoader
  4. */
  5. private static boolean isSourceNewer(URL source, ClassNode cls) {
  6. try {
  7. long lastMod;
  8. // Special handling for file:// protocol, as getLastModified() often reports
  9. // incorrect results (-1)
  10. if (source.getProtocol().equals("file")) {
  11. // Coerce the file URL to a File
  12. String path = source.getPath().replace('/', File.separatorChar).replace('|', ':');
  13. File file = new File(path);
  14. lastMod = file.lastModified();
  15. } else {
  16. URLConnection conn = source.openConnection();
  17. lastMod = conn.getLastModified();
  18. conn.getInputStream().close();
  19. }
  20. return lastMod > getTimeStamp(cls);
  21. } catch (IOException e) {
  22. // if the stream can't be opened, let's keep the old reference
  23. return false;
  24. }
  25. }
  26. }

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

  1. @Override
  2. public long lastModified() throws IOException {
  3. URL url = getURL();
  4. if (ResourceUtils.isFileURL(url) || ResourceUtils.isJarURL(url)) {
  5. // Proceed with file system resolution...
  6. return getFile().lastModified();
  7. }
  8. // Try a URL connection last-modified header...
  9. URLConnection con = url.openConnection();
  10. useCachesIfNecessary(con);
  11. if (con instanceof HttpURLConnection) {
  12. ((HttpURLConnection) con).setRequestMethod("HEAD");
  13. }
  14. return con.getLastModified();
  15. }

相关文章

URLConnection类方法