1、准备待下载文件
2、准备下载地址
三、核心代码
/** * java 多线程实现文件下载 * @author gaojignsong * */ public class multidownload { public static string path = "http://localhost:8080/upload/g4studio.pdf";// 下载文件的路径 public static string store = "e:/c/g4.pdf";// 文件存储路径 public static string temp = "e:/c/";// 临时文件的存储路径 public static string method = "get"; public static int threadnum = 5;// 设置开启的线程数量 /** * @param args */ public static void main(string[] args) { httpurlconnection connection = getconnection(path, method); try { connection.connect();// 打开连接 if (connection.getresponsecode() == 200) { // 获取文件的大小 int length = connection.getcontentlength(); system.out.println(length); file file = new file(store); // 生成临时文件 randomaccessfile raf = new randomaccessfile(file, "rwd"); // 设置临时文件的大小 raf.setlength(length); int blocksize = length / threadnum; for (int i = 0; i < threadnum; i ) { int startindex = i * blocksize ; int endindex = (i 1) * blocksize - 1; if (i == threadnum - 1) endindex = length - 1; system.out.println("start: " startindex " end: " endindex); new downloadthread(startindex, endindex, i).start(); } } } catch (ioexception e) { e.printstacktrace(); } } /** * 获取连接 * @param path * @return */ public static httpurlconnection getconnection(string path, string method) { httpurlconnection connection = null; try { url url = new ; connection = (httpurlconnection) url.openconnection(); connection.setreadtimeout(6000);// 设置读取超时时间 connection.setconnecttimeout(6000);// 设置连接超时时间 connection.setrequestmethod(method); } catch (exception e) { e.printstacktrace(); } return connection; } } class downloadthread extends thread { public int startindex; public int endindex; public int threadid; public downloadthread(int startindex, int endindex, int threadid) { this.startindex = startindex; this.endindex = endindex; this.threadid = threadid; } @override public void run() { //创建保存下载进度的临时文件 file progressfile = new file(multidownload.temp threadid ".txt"); //如果进度临时文件存在 if (progressfile.exists()) { try { bufferedreader reader = new bufferedreader(new filereader(progressfile)); //从进度临时文件中读取出上一次下载的总进度 //然后与原本的开始位置相加,得到新的开始位置 startindex = integer.parseint(reader.readline());// 计算出断点需要开始下载的字节位置 reader.close(); } catch (exception e) { e.printstacktrace(); } } httpurlconnection connection = multidownload.getconnection( multidownload.path, multidownload.method); //设置本次http请求所请求的数据的区间 connection.setrequestproperty("range", "bytes=" startindex "-" endindex); try { connection.connect(); int code = connection.getresponsecode(); system.out.println(code); //创建一个临时文件 ,拿到临时文件的输出流 randomaccessfile randomaccessfile = new randomaccessfile(new file(multidownload.store), "rw"); //把文件的写入位置移动至startindex randomaccessfile.seek(startindex); //200:请求全部资源成功 206:代表部分资源请求成功 if (connection.getresponsecode() == 206) { inputstream is = connection.getinputstream(); byte[] buffer = new byte[2048]; int len = 0; int total = 0; while ((len = is.read(buffer)) != -1) { //每次读取流里数据之后,同步把数据写入临时文件 randomaccessfile.write(buffer, 0, len); total = len; //将执行进度写入临时文件中,生成一个专门用来记录下载进度的临时文件 randomaccessfile tempraf = new randomaccessfile(progressfile, "rwd"); tempraf.write((total "").getbytes()); tempraf.close(); } } // 只要线程执行到这一步的话,就已经表明他已经完成了自己应该爬取的字节范围啦,所以要删除临时文件 progressfile.delete(); } catch (exception e) { e.printstacktrace(); } } }
四、结果验证
相关推荐
java实现多线程下载文件功能,通过线程池管理,实现下载文件速度快,安全。
刚学完多线程和线程池,以及线程爱好者。 使用场景及目标: 大文件的快速下载,经测试在100m带宽的网络下,下载一个40m的文件,不超过两秒钟,当然还得根据自己电脑的配置来讲。 其他说明: 文件如果特别小,而...
java多线程断点下载文件 ,文档介绍及代码实现
利用java技术实现多线程文件传输
实现了ftp多线程下载,并有简单的包括进度条的界面,代码的运行:1 把自己的pc配置成ftp服务器(只需下载serc-u软件)2 建立用户,用户名:zhangyu,密码:123,设置根目录,并拷贝进一些文件(这就是ftp服务器上的要...
java多线程加队列上传文件_后台处理
实现文件多线程下载,可以指定下载线程数。可以用于学习
新鲜出炉的springboot版本的多线程下载文件,可以拿来整合springboot项目或者ssm项目,可直接运行测试!!!
1. 把每个下载文件切成若干个块...2. http header里增加range,如果服务器返回cotent-range 说明服务器支持文件定位,可以实现多线程下载 详细看这里 http://blog.csdn.net/maoxiang/archive/2010/04/02/5446293.aspx
实现java多线程切割网络文件下载 比如一个文件30m,分5个线程下载, 每个线程下载6m,并且从指定位置开始读取文件. nokyxdownloader.java =>void httpdownloadtolocalfile(string destfilename,string srcurl,int ...
基于java的多线程网络爬虫设计与实现.txt
针对操作系统中多线程部分知识开发的一个案例,有助于更好的理解线程
java ftp多线程 批量 断点续传,使用 1.建一个支持多线程ftp服务端; 如:xxftp有windows和linux版本; 2.建一个项目导入文件测试;
主要介绍了java多线程文件分片下载实现的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
java多线程复制文件
利用java多线程以及网络编程知识写的一个下载程序源码,程序根据被下载文件的大小启动相应个数的线程对其进行下载。
目前。...该文在7ava网络和多线程技术的基 础上,提出了一个简单而实用的文件分割算法,并据此设计开发了一个的简单实府的网络资源下栽工具.使用该下裁工具可以实现 对多种格式的网络文件的浏览和下栽
java实现多文件异步上传
如果您想从服务器端下载多个文件,若按照传统的下载方式,这下载速度是非常慢的,我这里提供一个把所有需要下载的文件先压缩,后下载的完全凯发k8国际娱乐官网入口的解决方案。在此秀一下,工大家参考之用。