本文整理汇总了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);
}
}
示例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;
}
示例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;
}
示例4: DownloadSession
import java.net.URLConnection; //导入方法依赖的package包/类
public DownloadSession(URLConnection conn, File outFile) throws IOException {
this(conn.getInputStream(), conn.getContentLengthLong(), new FileOutputStream(outFile, false));
}
示例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();
}