JavaSE多线程下载及断点续传

来源:互联网 发布:企业级软件 编辑:程序博客网 时间:2024/05/08 11:33

1、用到的知识点:

(1)Java多线程

(2)Java I/O

(3)一个重要的类:RandomAccessFile,该类可实现文件的随机读取,具体参见JDK。

(4)Java net相关知识

2、多线程下载的思路:

(1)首先请求服务器获取服务器端文件的大小(totalsize),在本地生成和服务器端文件一样大小的空文件。

(2)得到服务器文件的大小,决定开启几个线程(注意子线程并不是越多越好),这里我们举例子开启3个(threadnumber=3)。

(3)计算每个子线程的下载的文件块的大小,例如,

blocksize=totalsize/threadnumber;

blocksize:每个线程下载的文件块的大小

totalsize:服务器端文件的总大小,也就是本地空文件的大小,(1)中已经获取

threadnumber:开辟的子线程的个数

所以,各个子线程的下载文件块字节范围如下:

线程1:0~blocksize*1

线程2:blocksize*1~blocksize*2

线程3:blocksize*2~totalsize  (注意:最后一个子线程必须下载剩下的数据块大小)

这些都不用啰嗦,相信大家学过小学数学的都能理解。

3、断点续传的原理:

在各个子线程下载的过程中,要记录每个子线程当前下载的进度,并持久化到存储设备中,这样下次下载的时候,读取记录下载位置的文件即可继续从上次中断的位置开始下载。

4、光说有点抽象,结合代码:

<pre name="code" class="java">package com.demo.download;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.URL;public class Download {public static final String path = "http://localhost:8080/youdao.exe";//服务器端文件的下载地址public static void main(String[] args) throws Exception {URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5000);conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");int code = conn.getResponseCode();if (code == 200) {int len = conn.getContentLength();RandomAccessFile file = new RandomAccessFile(getFilenName(path),"rwd");// 1.设置本地文件大小跟服务器的文件大小一致file.setLength(len);// 2 .假设开启3 个线程int threadnumber = 3;int blocksize = len / threadnumber;/** * 线程1 0~ blocksize <span style="white-space:pre"></span> * 线程2 1*bolocksize ~ 2*blocksize <span style="white-space:pre"></span> * 线程3 2*blocksize ~文件末尾 */for (int i = 0; i < threadnumber; i++) {int startposition = i * blocksize;int endpositon = (i + 1) * blocksize;if (i == (threadnumber - 1)) {// 最后一个线程endpositon = len;}DownLoadTask task = new DownLoadTask(i, path, startposition,endpositon);task.start();}}}<span style="white-space:pre"></span>//得到服务器端文件的名字public static String getFilenName(String path) {int start = path.lastIndexOf("/") + 1;return path.substring(start, path.length());}}//下载任务,各个子线程公共执行的代码class DownLoadTask extends Thread {public static final String path = "http://localhost:8080/youdao.exe";int threadid;String filepath;int startposition;int endpositon;public DownLoadTask(int threadid, String filepath, int startposition,int endpositon) {this.threadid = threadid;this.filepath = filepath;this.startposition = startposition;this.endpositon = endpositon;}@Overridepublic void run() {try {// 创建一个文件对象 ,记录当前某个文件的下载位置File postionfile = new File(threadid + ".txt");URL url = new URL(filepath);HttpURLConnection conn = (HttpURLConnection) url.openConnection();if (postionfile.exists()) {FileInputStream fis = new FileInputStream(postionfile);byte[] result = StreamTool.getBytes(fis);//这里的Integer转换是存在安全隐患的/** * 什么情况下转换失败?即positionfile中存的是空字符串的时候
<span style="white-space:pre"></span> * 什么情况下positionfile中存的是空字符串?positionfile刚被创建出来还没来得及存下载进度的时候中断了下载。
<span style="white-space:pre"></span> * 所以再读取positionfile时先要判断其中是否存储了下载的进度值 */String newstrstartposition=new String(result);int newstartposition=0;if(!("".equals(newstrstartposition))){newstartposition = Integer.parseInt(newstrstartposition);}if (newstartposition > startposition) {startposition = newstartposition;}}System.out.println("线程" + threadid + "正在下载 " + "开始位置 : "+ startposition + "结束位置 " + endpositon);// "Range", "bytes=2097152-4194303")//设置请求的数据范围,这是http协议规定的格式conn.setRequestProperty("Range", "bytes=" + startposition + "-"+ endpositon);conn.setRequestMethod("GET");conn.setConnectTimeout(5000);conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");InputStream is = conn.getInputStream();RandomAccessFile file = new RandomAccessFile(getFilenName(path),"rwd");// 设置 数据从文件哪个位置开始写file.seek(startposition);byte[] buffer = new byte[1024];int len = 0;// 代表当前读到的服务器数据的位置 ,同时这个值已经存储的文件的位置int currentPostion = startposition;while ((len = is.read(buffer)) != -1) {file.write(buffer, 0, len);currentPostion += len;// 需要把currentPostion 信息给持久化到存储设备String position = currentPostion + "";FileOutputStream fos = new FileOutputStream(postionfile);fos.write(position.getBytes());fos.flush();fos.close();}file.close();System.out.println("线程" + threadid + "下载完毕");// 当线程下载完毕后 把文件删除掉if (postionfile.exists()) {postionfile.delete();}} catch (Exception e) {e.printStackTrace();}super.run();}public static String getFilenName(String path) {int start = path.lastIndexOf("/") + 1;return path.substring(start, path.length());}}

StreamToo.java

package com.demo.download;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;public class StreamTool {public static byte[]getBytes(InputStream is) throws IOException{byte[]result=null;ByteArrayOutputStream baos=new ByteArrayOutputStream();int len=-1;byte[]buffer=new byte[1024];while((len=is.read(buffer))!=-1){baos.write(buffer, 0, len);}baos.flush();result=baos.toByteArray();baos.close();is.close();return result;}}


0 0
原创粉丝点击