package com.hbsi.csdn.multile;
import java.io.ByteArrayOutputStream;
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 test {
public static final String path = "http://192.168.64.132:8080/youdaocidian.exe";
public static int threadcount;
public static void main(String[] args) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");// 设置请求方式
conn.setConnectTimeout(5000); // 设置获取响应码的时间
int len = conn.getContentLength(); // 获取相应数据的长度
System.out.println("下载的文件的长度是:" + len);
// 在客户端创建一个大小跟服务区端文件一样的资源
File file = new File("youdao.exe");
RandomAccessFile randomfile = new RandomAccessFile(file, "rwd");
randomfile.setLength(len);
// 假设开启5个子线程
int blocksize = len / 5;
threadcount=0;
for (int i = 1; i <= 5; i++) {
int startsize = (i - 1) * blocksize;
int endsize = (i) * blocksize - 1;
if (i == 5) {
endsize = len;
}
new Thread(new DownLoadTask(i, startsize, endsize)).start();
//解决所有的子线程全部执行完毕后,在把所有的i.txt文件删除
}
}
}
class DownLoadTask implements Runnable {
private int id, startsize, endsize;
public DownLoadTask(int id, int startsize, int endsize) {
super();
this.id = id; // 线程的id
this.startsize = startsize;
this.endsize = endsize;
}
@Override
public void run() {
try {
//每一个线程创建执行时都创建一个id.txt的文件,这个文件用来记载当前线程的下载进度。
File idfile=new File(id+".txt");
if(idfile.exists()){
FileInputStream fis=new FileInputStream(idfile);
ByteArrayOutputStream boas=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))!=-1){
boas.write(buffer,0,len);
}
fis.close();
boas.close();
byte[] result=boas.toByteArray();
String numberstr=new String(result);
if(numberstr!=null&&(!"".equals(numberstr))){
int startposition=Integer.parseInt(numberstr);
if(startposition>startsize){
startsize=startposition; //重新指定下载的开始位置
}
}
}
URL url = new URL(test.path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");// 设置请求方式
conn.setConnectTimeout(5000); // 设置获取响应码的时间
//指定当前线程从服务器上的哪个位置下载
conn.setRequestProperty("Range", "bytes="+startsize+"-"+endsize);
System.out.println("文件下载的开始位置:"+startsize+" 结束位置:"+endsize);
InputStream is=conn.getInputStream();
File file = new File("youdao.exe");
RandomAccessFile randomfile = new RandomAccessFile(file, "rwd");
randomfile.seek(startsize);
byte[] buffer=new byte[1024];
int len=0;
int tatol=0;
while((len=is.read(buffer))!=-1){
randomfile.write(buffer,0,len);
tatol+=len;
FileOutputStream idfos=new FileOutputStream(idfile);
idfos.write((""+(startsize+tatol)).getBytes());
idfos.flush();
idfos.close();
}
randomfile.close();
is.close();
System.out.println("线程 "+id+" 下载完毕");
/* if(idfile.exists()){
idfile.delete();
}*/
synchronized(test.class){
test.threadcount++;
if(test.threadcount>=5){
for(int i=1;i<=5;i++){
File deletefile=new File(i+".txt");
deletefile.delete();
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.ByteArrayOutputStream;
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 test {
public static final String path = "http://192.168.64.132:8080/youdaocidian.exe";
public static int threadcount;
public static void main(String[] args) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");// 设置请求方式
conn.setConnectTimeout(5000); // 设置获取响应码的时间
int len = conn.getContentLength(); // 获取相应数据的长度
System.out.println("下载的文件的长度是:" + len);
// 在客户端创建一个大小跟服务区端文件一样的资源
File file = new File("youdao.exe");
RandomAccessFile randomfile = new RandomAccessFile(file, "rwd");
randomfile.setLength(len);
// 假设开启5个子线程
int blocksize = len / 5;
threadcount=0;
for (int i = 1; i <= 5; i++) {
int startsize = (i - 1) * blocksize;
int endsize = (i) * blocksize - 1;
if (i == 5) {
endsize = len;
}
new Thread(new DownLoadTask(i, startsize, endsize)).start();
//解决所有的子线程全部执行完毕后,在把所有的i.txt文件删除
}
}
}
class DownLoadTask implements Runnable {
private int id, startsize, endsize;
public DownLoadTask(int id, int startsize, int endsize) {
super();
this.id = id; // 线程的id
this.startsize = startsize;
this.endsize = endsize;
}
@Override
public void run() {
try {
//每一个线程创建执行时都创建一个id.txt的文件,这个文件用来记载当前线程的下载进度。
File idfile=new File(id+".txt");
if(idfile.exists()){
FileInputStream fis=new FileInputStream(idfile);
ByteArrayOutputStream boas=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))!=-1){
boas.write(buffer,0,len);
}
fis.close();
boas.close();
byte[] result=boas.toByteArray();
String numberstr=new String(result);
if(numberstr!=null&&(!"".equals(numberstr))){
int startposition=Integer.parseInt(numberstr);
if(startposition>startsize){
startsize=startposition; //重新指定下载的开始位置
}
}
}
URL url = new URL(test.path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");// 设置请求方式
conn.setConnectTimeout(5000); // 设置获取响应码的时间
//指定当前线程从服务器上的哪个位置下载
conn.setRequestProperty("Range", "bytes="+startsize+"-"+endsize);
System.out.println("文件下载的开始位置:"+startsize+" 结束位置:"+endsize);
InputStream is=conn.getInputStream();
File file = new File("youdao.exe");
RandomAccessFile randomfile = new RandomAccessFile(file, "rwd");
randomfile.seek(startsize);
byte[] buffer=new byte[1024];
int len=0;
int tatol=0;
while((len=is.read(buffer))!=-1){
randomfile.write(buffer,0,len);
tatol+=len;
FileOutputStream idfos=new FileOutputStream(idfile);
idfos.write((""+(startsize+tatol)).getBytes());
idfos.flush();
idfos.close();
}
randomfile.close();
is.close();
System.out.println("线程 "+id+" 下载完毕");
/* if(idfile.exists()){
idfile.delete();
}*/
synchronized(test.class){
test.threadcount++;
if(test.threadcount>=5){
for(int i=1;i<=5;i++){
File deletefile=new File(i+".txt");
deletefile.delete();
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}