package top.mengmei219;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class DownLoad {
static int threadCound = 3; //线程数
static int size; //每个线程分的大小
static int finishThread = 0; //活动的线程数
//多线程断点续传 下载
public static void main(String[] args) {
try {
URL url = new URL("http://192.168.1.104:8080/itheima74/lczd.pdf");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int contentLength = urlConnection.getContentLength();
RandomAccessFile random = new RandomAccessFile("/Users/mengmei/Desktop/lang.pdf", "rw");
random.setLength(contentLength);
for(int threadID=0; threadID<threadCound; threadID++){
size = contentLength/threadCound;
int startPosition = threadID*size;
int endPosition = (threadID+1)*size-1;
if(threadID == threadCound-1){
endPosition = contentLength-1;
}
new MyThread(threadID, startPosition, endPosition).start();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static class MyThread extends Thread{
private int threadID;
private int startPosition;
private int endPosition;
public MyThread(int threadID, int startPosition, int endPosition) {
this.threadID = threadID;
this.startPosition = startPosition;
this.endPosition = endPosition;
System.out.println("threadID"+threadID+": "+startPosition+" - "+endPosition);
}
@Override
public void run() {
int lastPosition = startPosition;
try {
URL url = new URL("http://192.168.1.104:8080/itheima74/lczd.pdf");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(1000*10);
File tempFile = new File("/Users/mengmei/Desktop/"+threadID+".txt");
if(tempFile.exists()){
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(tempFile)));
String line = reader.readLine();
lastPosition = Integer.parseInt(line);
startPosition = lastPosition;
reader.close();
}
//分段请求头
urlConnection.setRequestProperty("Range", "bytes="+startPosition+"-"+endPosition);
int code = urlConnection.getResponseCode();
if (code == 206) { //分段请求成功
InputStream inputStream = urlConnection.getInputStream();
byte[] buffer = new byte[1024*10];
int len = 0;
RandomAccessFile random = new RandomAccessFile("/Users/mengmei/Desktop/lang.pdf", "rw");
random.seek(startPosition);
while ( (len=inputStream.read(buffer)) > 0 ) {
random.write(buffer, 0, len);
lastPosition = lastPosition + len;
FileOutputStream tempOut = new FileOutputStream(tempFile);
tempOut.write(String.valueOf(lastPosition).getBytes());
tempOut.flush();
tempOut.close();
}
inputStream.close();
random.close();
}
synchronized (MyThread.class) {
System.out.println(threadID+" - 下载完成!");
finishThread ++;
if (finishThread == threadCound) {
System.out.println("全部下载完成,删掉临时文件!");
for(int i=0; i<threadCound; i++){
File temp = new File("/Users/mengmei/Desktop/"+threadID+".txt");
if (!temp.delete()) {
temp.deleteOnExit(); //在JVM退出时删除
}
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
JavaSE-多线程下载
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.普通单线程下载文件: 直接使用URLConnection.openStream();打开网络输入流,然后将流写...
- 使用第三方框架#import "AFNetworking.h"下载网络Json数据 使用第三方框架#import ...
- 一、为什么要使用多线程,多线程真的能提高效率吗? 1.1为什么要使用多线程 多线程编程的目的,就是"最大限度地利用...