您的位置:首页 > 其它

多线程断点下载实现(J2SE版本)

2012-11-25 14:56 253 查看
package cn.itcast.download;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class TestDownload {

public static final String path = "http://localhost:8080/youdao.exe";

public static void main(String[] args) throws IOException {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(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();
//			创建从中读取和向其中写入(可选)的随机访问文件流,该文件具有指定名称。
//			"rwd"   打开以便读取和写入,对于 "rw",还要求对文件内容的每个更新都同步写入到底层存储设备。
RandomAccessFile file = new RandomAccessFile(
getFileName(path), "rwd");
// 1.设置本地文件大小跟服务器的文件大小一致
file.setLength(len);

// 2 .假设开启3 个线程
int threadnumber = 3;
int blocksize = len / threadnumber;
/**
* 线程1 0~ blocksize
* 线程2 1*bolocksize ~ 2*blocksize
* 线程3 2*blocksize ~
* 文件末尾
*/
for (int i = 0; i < threadnumber; i++) {
int startpostion = i*blocksize;
int endposition = (i+1)*blocksize;
if (i == (threadnumber - 1)) {
endposition = len;
}
//开始为每个线程下载。

DownLoadTask task = new DownLoadTask(i, path, startpostion, endposition);

task.start();

//				在启动线程时候,有两种方法可以使用
//				start()为一半通用的启动线程的方法,通过调用线程类Thread的start()方法来启动线程,
//				然后线程Thread类通过调用run()方法来实现线程的操作,而其中的run()方法中的内容实为线程体,
//				即为该线程所要执行的内容。run()方法执行结束,该线程也就终止了,cpu在执行其他的线程
//				如果将线程看做一般的Java类的话,则run()方法相当于普通的方法调用
//				,通过普通的java类实例.run()来启动线程,即执行该方法体的内容。最终程序执行的只有主线程这一个。

}

}

}
/**
* @param path
* @return
* 根据服务器上的路径得到文件名称。
*/
public static String getFileName(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;
}

@Override
public void run() {
try {
//引入断点下载  这个文件用于记录该线程下载到了哪个位置,有几个线程就会产生几个这样的文件。
File positionFile = new File(threadid+".txt");

URL url = new URL(filepath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

System.out.println("线程" + threadid + "正在下载 " + "开始位置 : "
+ startposition + "结束位置 " + endpositon);

//执行这句话就说明该线程已经是第二次之后执行到这了。
if (positionFile.exists()) {
FileInputStream fis = new FileInputStream(positionFile);
byte[] result = StreamUtil.getBytes(fis);
int newstartposition = Integer.parseInt(new String(result));
if (newstartposition > startposition) {
startposition = newstartposition;
}
}
// "Range", "bytes=2097152-4194303")
connection.setRequestProperty("Range", "bytes="+startposition+"-"+endpositon);
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
InputStream iStream = connection.getInputStream();
RandomAccessFile file = new RandomAccessFile(getFileName(path), "rwd");
// 设置 数据从文件哪个位置开始写
file.seek(startposition);
byte[] bytes = new byte[1024];
int len = 0;
// 代表当前读到的服务器数据的位置 ,同时这个值已经存储的文件的位置
int currentPostion = startposition;
// 创建一个文件对象 ,记录当前某个文件的下载位置
while ((len = iStream.read(bytes)) != -1) {
file.write(bytes, 0, len);

currentPostion += len;
// 需要把currentPostion 信息给持久化到存储设备
String position = currentPostion + "";
FileOutputStream fos = new FileOutputStream(positionFile);
fos.write(position.getBytes());
fos.flush();
fos.close();
}
file.close();

System.out.println("线程" + threadid + "下载完毕");

// 当线程下载完毕后 把文件删除掉
if (positionFile.exists()) {
positionFile.delete();
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

public static String getFileName(String path){
int start = path.lastIndexOf("/") + 1;
return path.substring(start, path.length());
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: