当前位置: 首页>>代码示例>>Java>>正文


Java URLConnection.getContentLengthLong方法代码示例

本文整理汇总了Java中java.net.URLConnection.getContentLengthLong方法的典型用法代码示例。如果您正苦于以下问题:Java URLConnection.getContentLengthLong方法的具体用法?Java URLConnection.getContentLengthLong怎么用?Java URLConnection.getContentLengthLong使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.URLConnection的用法示例。


在下文中一共展示了URLConnection.getContentLengthLong方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: create

import java.net.URLConnection; //导入方法依赖的package包/类
/**
 * Creates a new URL resource for the given URL.
 * The URL must point to an existing and valid file.
 * A connection will be opened to that URL to query lastModified and content size metadata.
 * 
 * @param url the URL to create the IO resource for
 * @return the created resource
 * @throws IOException if querying metadata fails
 */
public static URLResource create(URL url) throws IOException {
	try {
		URLConnection connection = url.openConnection();
		
		long lastModified = connection.getLastModified();
		if(lastModified==0) {
			throw new IOException("lastModified is unknown");
		}
		
		long size = connection.getContentLengthLong();
		if(size<0) {
			throw new IOException("content size is unknown");
		}
		
		return new URLResource(url, lastModified, size);
		
	} catch(Exception e) {
		throw new IOException("Failed to query metadata for URL '" + url + "'", e);
	}
}
 
开发者ID:nthuemmel,项目名称:executable-packer-maven-plugin,代码行数:30,代码来源:URLResource.java

示例2: readBytes

import java.net.URLConnection; //导入方法依赖的package包/类
/**
 * 
 */
public static byte[] readBytes(URL url) throws IOException {
    if (url != null) {
        URLConnection connection = url.openConnection();
        connection.connect();
        
        long contentLength = connection.getContentLengthLong();
        
        if (contentLength > MAX_ARRAY_SIZE) {
            throw new IOException("stream length is too large");
        }
        
        try (InputStream inputStream = connection.getInputStream()) {
            int available = inputStream.available();
            
            if (available > 0) {
                return readBytes(inputStream,available > contentLength ?
                        available : (int)contentLength);
            }
        }
    }
    
    return EMPTY_BYTE_ARRAY;
}
 
开发者ID:annoflex,项目名称:annoflex,代码行数:27,代码来源:SystemToolkit.java

示例3: download

import java.net.URLConnection; //导入方法依赖的package包/类
/**
 * Download the resource specified by its URI to the target directory using the provided file name.
 */
Path download(URI uri, Path targetDirectory, String targetFileName, Predicate<Path> useTimeStamp) throws IOException {
  URL url = requireNonNull(uri, "uri must not be null").toURL();
  requireNonNull(targetDirectory, "targetDirectory must be null");
  if (requireNonNull(targetFileName, "targetFileName must be null").isEmpty()) {
    throw new IllegalArgumentException("targetFileName must be blank");
  }
  Files.createDirectories(targetDirectory);
  Path targetPath = targetDirectory.resolve(targetFileName);
  URLConnection urlConnection = url.openConnection();
  FileTime urlLastModifiedTime = FileTime.fromMillis(urlConnection.getLastModified());
  if (Files.exists(targetPath)) {
    if (Files.getLastModifiedTime(targetPath).equals(urlLastModifiedTime)) {
      if (Files.size(targetPath) == urlConnection.getContentLengthLong()) {
        if (useTimeStamp.test(targetPath)) {
          log.log(Level.FINE, "download skipped - using `%s`%n", targetPath);
          return targetPath;
        }
      }
    }
    Files.delete(targetPath);
  }
  log.log(Level.FINE, "download `%s` in progress...%n", uri);
  try (InputStream sourceStream = url.openStream(); OutputStream targetStream = Files.newOutputStream(targetPath)) {
    sourceStream.transferTo(targetStream);
  }
  Files.setLastModifiedTime(targetPath, urlLastModifiedTime);
  log.log(Level.CONFIG, "download `%s` completed%n", uri);
  log.info("stored `%s` [timestamp=%s]%n", targetPath, urlLastModifiedTime.toString());
  return targetPath;
}
 
开发者ID:sormuras,项目名称:bach,代码行数:34,代码来源:Bach-06.java

示例4: DownloadSession

import java.net.URLConnection; //导入方法依赖的package包/类
public DownloadSession(URLConnection conn, File outFile) throws IOException {
    this(conn.getInputStream(), conn.getContentLengthLong(), new FileOutputStream(outFile, false));
}
 
开发者ID:upperlevel,项目名称:uppercore,代码行数:4,代码来源:DownloadSession.java

示例5: getContentLength

import java.net.URLConnection; //导入方法依赖的package包/类
public static long getContentLength(URLConnection connection) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return connection.getContentLengthLong();
    }
    return connection.getContentLength();
}
 
开发者ID:kalikov,项目名称:lighthouse,代码行数:7,代码来源:NetworkUtils.java


注:本文中的java.net.URLConnection.getContentLengthLong方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。