本文整理匯總了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;
}