当前位置: 首页>>代码示例>>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;未经允许,请勿转载。