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

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

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

URLConnection.getContentLengthLong介绍

[英]Returns the value of the content-length header field as a long.
[中]将content-length标题字段的值作为长字符串返回。

代码示例

代码示例来源:origin: MovingBlocks/Terasology

  1. private Map<URL, Long> getSizes() throws IOException {
  2. Map<URL, Long> result = new HashMap<>();
  3. int current = 1;
  4. int total = urlToTargetMap.size();
  5. for (URL url: urlToTargetMap.keySet()) {
  6. progressListener.onSizeMetadataProgress(current, total);
  7. URLConnection conn = url.openConnection();
  8. result.put(url, conn.getContentLengthLong());
  9. current++;
  10. }
  11. return result;
  12. }

代码示例来源: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: spring-projects/spring-framework

  1. @Override
  2. public long contentLength() throws IOException {
  3. URL url = getURL();
  4. if (ResourceUtils.isFileURL(url)) {
  5. // Proceed with file system resolution
  6. File file = getFile();
  7. long length = file.length();
  8. if (length == 0L && !file.exists()) {
  9. throw new FileNotFoundException(getDescription() +
  10. " cannot be resolved in the file system for checking its content length");
  11. }
  12. return length;
  13. }
  14. else {
  15. // Try a URL connection content-length header
  16. URLConnection con = url.openConnection();
  17. customizeConnection(con);
  18. return con.getContentLengthLong();
  19. }
  20. }

代码示例来源: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: wildfly/wildfly

  1. lastModified = new Date(connection.getLastModified());
  2. contentLength = connection.getContentLengthLong();
  3. } finally {
  4. if (connection != null) {

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

  1. long contentLength = con.getContentLengthLong();
  2. if (contentLength > 0) {
  3. return true;

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

  1. /**
  2. * Immediately opens a connection to the given URL to retrieve
  3. * data about the connection, including the input stream.
  4. *
  5. * @param url The URL to resource
  6. */
  7. public StreamedFile(URL url) {
  8. String path = url.getPath();
  9. int idx = path.lastIndexOf(File.separatorChar);
  10. this.name = idx > -1 ? path.substring(idx + 1) : path;
  11. this.mediaType = MediaType.forFilename(name);
  12. try {
  13. URLConnection con = url.openConnection();
  14. this.lastModified = con.getLastModified();
  15. this.inputStream = con.getInputStream();
  16. this.length = con.getContentLengthLong();
  17. } catch (IOException e) {
  18. throw new CustomizableResponseTypeException("Could not open a connection to the URL: " + path, e);
  19. }
  20. }

代码示例来源: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: spring-projects/spring-framework

  1. if (con.getContentLengthLong() > 0) {
  2. return true;

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

  1. if (con.getContentLengthLong() >= 0) {
  2. return true;

代码示例来源:origin: org.springframework/spring-core

  1. @Override
  2. public long contentLength() throws IOException {
  3. URL url = getURL();
  4. if (ResourceUtils.isFileURL(url)) {
  5. // Proceed with file system resolution
  6. File file = getFile();
  7. long length = file.length();
  8. if (length == 0L && !file.exists()) {
  9. throw new FileNotFoundException(getDescription() +
  10. " cannot be resolved in the file system for checking its content length");
  11. }
  12. return length;
  13. }
  14. else {
  15. // Try a URL connection content-length header
  16. URLConnection con = url.openConnection();
  17. customizeConnection(con);
  18. return con.getContentLengthLong();
  19. }
  20. }

代码示例来源:origin: org.springframework/spring-core

  1. long contentLength = con.getContentLengthLong();
  2. if (contentLength > 0) {
  3. return true;

代码示例来源:origin: org.springframework/spring-core

  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.springframework/spring-core

  1. if (con.getContentLengthLong() > 0) {
  2. return true;

代码示例来源:origin: org.jboss.modules/jboss-modules

  1. public long getSize() {
  2. final long len = connection.getContentLengthLong();
  3. return len == -1 ? 0 : len;
  4. }
  5. }

代码示例来源:origin: org.apache.tomcat/tomcat-catalina

  1. @Override
  2. public long getContentLengthLong() {
  3. return wrappedJarUrlConnection.getContentLengthLong();
  4. }

代码示例来源:origin: com.google.enterprise.cloudsearch/google-cloudsearch-indexing-connector-sdk

  1. public UrlInputStreamContent(String type, URL url) throws IOException {
  2. super(type);
  3. this.url = url;
  4. this.urlConnection = url.openConnection();
  5. this.length =
  6. urlConnection.getContentLengthLong() == -1L ? 0L : urlConnection.getContentLengthLong();
  7. }

代码示例来源:origin: com.github.subchen/jetbrick-commons

  1. @Override
  2. public long length() {
  3. try {
  4. return url.openConnection().getContentLengthLong();
  5. } catch (IOException e) {
  6. throw new IllegalStateException(e);
  7. }
  8. }

代码示例来源:origin: org.apache.tomcat/tomcat-catalina

  1. @Override
  2. public long getFileSize(String path, boolean virtual) throws IOException {
  3. long fileSize = -1;
  4. try {
  5. URLConnection urlConnection = getURLConnection(path, virtual);
  6. fileSize = urlConnection.getContentLengthLong();
  7. } catch (IOException e) {
  8. // Ignore this. It will always fail for non-file based includes
  9. }
  10. return fileSize;
  11. }

代码示例来源:origin: octo-online/reactive-audit

  1. @Test(expected = NetworkReactiveAuditException.class)
  2. public void getContentLengthLong()
  3. throws IOException
  4. {
  5. Assume.assumeTrue(IOTestTools.isNetworkConnected());
  6. ReactiveAudit.off.commit();
  7. URLConnection conn = new URL("http://" + HOST + ":" + PORT).openConnection();
  8. TestTools.strict.commit();
  9. conn.getContentLengthLong();
  10. }

相关文章

URLConnection类方法