影响下载的速度

* 宽带的带宽

* 服务器的限制

* 服务器的资源固定,开启的线程越多抢占的资源就越多

import java.io.InputStream;

import java.io.RandomAccessFile;

import java.net.HttpURLConnection;

import java.net.URL;

public class MultiDownLoad {

   static String path = http://192.168.3.100:8080/web/download/gui.exe;
static int threadCount = 3; /**
* 获取文件的存储路径
*/ static String getFilePath(){ int index = path.lastIndexOf("/")+1; return "D:\\"+path.substring(index); }
public static void main(String[] args) { try {
// 1. 在客户端创建和服务器资源一样大小的空文件 URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(3000); conn.setRequestMethod("GET"); int code = conn.getResponseCode(); //服务器资源文件的大小 int fileLength = 0; if (code == 200) { fileLength = conn.getContentLength(); System.out.println("文件大小:"+fileLength); // //可选,可以不写,检测硬盘的空间够不够用 // RandomAccessFile raf = new RandomAccessFile(getFilePath(), "rw"); // //在硬盘上创建控件 // raf.setLength(fileLength); // raf.close(); } //每个线程下载的区块大小 int blockSize = fileLength / threadCount; // 2. 客户端开启多个线程去下载服务器的资源 for (int threadId = 0; threadId < threadCount; threadId++) { int startIndex = threadId * blockSize; int endIndex = (threadId + 1)* blockSize -1; //最后一个线程,修正下载的结束位置 if (threadId == threadCount-1) { endIndex = fileLength - 1; } //开启线程 new DownLoadThread(startIndex, endIndex, threadId).start(); } } catch (Exception e) { e.printStackTrace(); } } static class DownLoadThread extends Thread { //开始位置 int startIndex; //结束位置 int endIndex; //线程ID int threadId; public DownLoadThread(int startIndex, int endIndex, int threadId) { super(); this.startIndex = startIndex; this.endIndex = endIndex; this.threadId = threadId; } @Override public void run() { super.run(); System.out.println("线程 : "+ threadId + " : "+ startIndex+" ~ "+endIndex); try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(3000); //重要,设置请求的范围 conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex); //部分请求成功 206 int code = conn.getResponseCode(); System.out.println(" code = "+code); if (code == 206) { RandomAccessFile raf = new RandomAccessFile(getFilePath(), "rw"); //重要,写文件之前定位 raf.seek(startIndex); //获取这个线程对应的一块资源 InputStream is = conn.getInputStream(); byte[] buffer = new byte[1024*8]; int len = -1; while((len = is.read(buffer)) != -1){ raf.write(buffer, 0, len); } raf.close();
} // 3. 每个线程都下载完毕,整个资源就下载完了 System.out.println("线程 "+threadId+" 干完活了!"); } catch (Exception e) { e.printStackTrace(); } } } }

HttpURLConnection 多线程下载的更多相关文章

  1. 使用HttpURLConnection多线程下载

    1 import java.io.IOException; 2 import java.io.InputStream; 3 import java.io.RandomAccessFile; 4 imp ...

  2. 多线程下载 HttpURLConnection

    Activity /**实际开发涉及文件上传.下载都不会自己写这些代码,一般会使用第三方库(如xUtils)或Android提供的DownloadManager下载*/ public class Ht ...

  3. 使用HttpURLConnection实现多线程下载

    HttpURLConnection继承了URLConnection,因此也可用于向指定网站发送GET请求.POST请求,而且它在URLConnection基础上提供了如下便捷方法: 实现多线程下载的步 ...

  4. java 多线程下载文件 以及URLConnection和HttpURLConnection的区别

    使用 HttpURLConnection 实现多线程下载文件 注意GET大写//http public class MultiThreadDownload { public static void m ...

  5. 【Java EE 学习 22 下】【单线程下载】【单线程断点下载】【多线程下载】

    一.文件下载简述 1.使用浏览器从网页上下载文件,Servlet需要增加一些响应头信息 (1)response.setContentType("application/force-downl ...

  6. Java--使用多线程下载,断点续传技术原理(RandomAccessFile)

    一.基础知识 1.什么是线程?什么是进程?它们之间的关系? 可以参考之前的一篇文章:java核心知识点学习----并发和并行的区别,进程和线程的区别,如何创建线程和线程的四种状态,什么是线程计时器 简 ...

  7. android 多线程下载 断点续传

    来源:网易云课堂Android极客班第八次作业练习 练习内容: 多线程 asyncTask handler 多线程下载的原理 首先获取到目标文件的大小,然后在磁盘上申请一块空间用于保存目标文件,接着把 ...

  8. 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)

    1.smartimageview使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  9. Java 仿迅雷多线程下载

    package net.webjoy.jackluo.android_json; /** * 1.http Range "bytes="+ start+end * 2.Random ...

随机推荐

  1. oracle to_Char fm 函数

    近期在使用oracle to_char函数处理浮点数时发现有坑,这里做个小结: 网上可以找到关于to_char中使用fm9990.0099中的相关解释: 0表示:如果参数(double或者float类 ...

  2. 【Day4】2.详解Http请求协议

    Http请求协议

  3. 【Day3】4.Xpath语法与案例

    课程目标 1.谷歌浏览器配置Xpath 2.Xpath常用语法 3.Xpath常用案例 1.谷歌浏览器配置Xpath Xpath下载:http://chromecj.com/web-developme ...

  4. 【异常】ERROR main:com.cloudera.enterprise.dbutil.SqlFileRunner: Exception while executing ddl scripts. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'ROLES' already exists

    1 详细异常 2019-10-11 10:33:55,865 INFO main:com.cloudera.server.cmf.Main: ============================= ...

  5. windows 8/10 彻底删除脱机文件

    先关闭脱机文件选项 控制面板 > 同步中心 > 管理脱机文件 > 查看脱机文件 > 点击 计算机 > ... > 脱机同步的文件夹右键,取消同步 可能需要重启 如果 ...

  6. Protocol handler start failedCaused by: java.net.SocketException: Permission denied

    最近在使用mac启动项目的时候,发现原本在Windows下正常跑的项目报错如下: Protocol handler start failedCaused by: java.net.SocketExce ...

  7. visual studio 和visual studio code 的区别是什么?

    区别有三: 区别一:含义不一样. Visual Studio(简称VS)是美国微软公司的开发工具包系列产品,是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码 ...

  8. JS 字符串转字节截取

    /* * param str 要截取的字符串 * param L 要截取的字节长度,注意是字节不是字符,一个汉字两个字节 * return 截取后的字符串 */ function CutStr(str ...

  9. move post process stack from package to asset

    这东西折腾了我好久 原来一直都是打开的方式不对 package 文件夹里面的manifest文件 改相应的package为文件路径引用     "com.unity.render-pipel ...

  10. git命令行提交流程

    一.顺利提交无冲突情况(diff->add->fetch->pull->commit->push) 1.git  status 查看状态 2. git diff head ...