當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpURLConnection.getContentLengthLong方法代碼示例

本文整理匯總了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);
    }
}
 
開發者ID:loom-build-tool,項目名稱:loom-installer,代碼行數:37,代碼來源:LoomInstaller.java

示例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;
}
 
開發者ID:PluginsCDTribe,項目名稱:MultiThreadDownloader,代碼行數:36,代碼來源:AsyncDownload.java

示例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;
}
 
開發者ID:IzzelAliz,項目名稱:LCL,代碼行數:36,代碼來源:AsyncDownload.java


注:本文中的java.net.HttpURLConnection.getContentLengthLong方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。