本文整理汇总了Java中java.net.HttpURLConnection.getContentLengthLong方法的典型用法代码示例。如果您正苦于以下问题:Java HttpURLConnection.getContentLengthLong方法的具体用法?Java HttpURLConnection.getContentLengthLong怎么用?Java HttpURLConnection.getContentLengthLong使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.HttpURLConnection
的用法示例。
在下文中一共展示了HttpURLConnection.getContentLengthLong方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: downloadZip
import java.net.HttpURLConnection; //导入方法依赖的package包/类
private static void downloadZip(final URL url, final Path target) throws IOException {
System.out.println("Downloading Loom Library from " + url + " ...");
final Path parent = target.getParent();
if (parent == null) {
throw new IllegalStateException();
}
final Path tmpFile = Files.createTempFile(parent, null, null);
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IllegalStateException("Connecting " + url + " resulted in "
+ conn.getHeaderField(0));
}
final long totalSize = conn.getContentLengthLong();
try (final InputStream inputStream = conn.getInputStream();
final OutputStream out = Files.newOutputStream(tmpFile,
StandardOpenOption.APPEND)) {
copy(inputStream, out, totalSize);
}
} finally {
conn.disconnect();
}
if (Files.notExists(target)) {
Files.move(tmpFile, target, StandardCopyOption.ATOMIC_MOVE);
}
}
示例2: download
import java.net.HttpURLConnection; //导入方法依赖的package包/类
public List<DownloadThread> download() throws Exception {
MTDownload.log("開始下載 " + path);
MTDownload.log("存储至 " + targetFilePath);
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(10000);
int code = connection.getResponseCode();
if (code == 200) {
long connectionLength = connection.getContentLengthLong();
length = connectionLength;
map.put("length", connection.getContentLengthLong());
MTDownload.log("文件大小: " + getSize(connection.getContentLengthLong()));
RandomAccessFile randomAccessFile = new RandomAccessFile(new File(targetFilePath, getFileName(url)), "rw");
randomAccessFile.setLength(connectionLength);
long blockSize = connectionLength / threadCount;
for (int threadId = 0; threadId < threadCount; threadId++) {
long startIndex = threadId * blockSize;
long endIndex = (threadId + 1) * blockSize - 1;
if (threadId == (threadCount - 1)) {
endIndex = connectionLength - 1;
}
DownloadThread dl = new DownloadThread(threadId, startIndex, endIndex);
dl.start();
threads.add(dl);
}
List<Thread> list = new ArrayList<>(threads);
list.add(Thread.currentThread());
tasks.add(list);
randomAccessFile.close();
}
return threads;
}
示例3: download
import java.net.HttpURLConnection; //导入方法依赖的package包/类
public List<DownloadThread> download() throws Exception {
callback.log("開始下載 " + path);
callback.log("存储至 " + targetFilePath);
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(10000);
int code = connection.getResponseCode();
if (code == 200) {
long connectionLength = connection.getContentLengthLong();
length = connectionLength;
map.put("length", connection.getContentLengthLong());
callback.log("文件大小: " + getSize(connection.getContentLengthLong()));
RandomAccessFile randomAccessFile = new RandomAccessFile(new File(targetFilePath, getFileName(url)), "rw");
randomAccessFile.setLength(connectionLength);
long blockSize = connectionLength / threadCount;
for (int threadId = 0; threadId < threadCount; threadId++) {
long startIndex = threadId * blockSize;
long endIndex = (threadId + 1) * blockSize - 1;
if (threadId == (threadCount - 1)) {
endIndex = connectionLength - 1;
}
DownloadThread dl = new DownloadThread(threadId, startIndex, endIndex, callback);
dl.start();
threads.add(dl);
}
List<Thread> list = new ArrayList<>(threads);
list.add(Thread.currentThread());
tasks.add(list);
randomAccessFile.close();
}
return threads;
}