下载器本体
package com.softeem.download;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
public class Download extends Thread {
/**
* 完成一个多线程下载工具 1.通过提供的网路文件地址,下载该文件 2.要求使用多个线程同时下载,并且实时更新下载进度(0%~100%)
* 3.要求能够实现断电功能(扩展)
*
* @param args
*/
private String str; //文件URL地址
private File dir; //目标文件夹
private File target; //目标
private long start; // 分段记录开始点
private long end; //分段记录结束点
public Download(String str, File dir, File target, long start, long end) {
super();
this.str = str;
this.dir = dir;
this.target = target;
this.start = start;
this.end = end;
}
//线程运行
public void run() {
int currentPosition = 0;
InputStream is = null;
RandomAccessFile raf = null;
long position = 0;
int count = 0;
System.out.println(this.getName() + "开始拷贝");
try {
URL url = new URL(str);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(10000);
is = conn.getInputStream();
raf = new RandomAccessFile(target, "rw");
position = readPosition(dir);
is.skip(start);
raf.seek(start);
byte[] b = new byte[1024];
int len = 0;
ProgressListener p = new ProgressListener(getlength(url) / 4,
currentPosition);
p.start();
while ((len = is.read(b)) != -1 && count <= (end - start)) {
currentPosition += len;
p.setCurrent(currentPosition);
count += len;
raf.write(b, 0, len);
sleep(100);
}
writePosition(position, dir);
System.out.println(this.getName() + "结束拷贝");
} catch (MalformedURLException e) {
writePosition(position, dir);
e.printStackTrace();
} catch (ProtocolException e) {
writePosition(position, dir);
e.printStackTrace();
} catch (FileNotFoundException e) {
writePosition(position, dir);
e.printStackTrace();
} catch (IOException e) {
writePosition(position, dir);
e.printStackTrace();
} catch (InterruptedException e) {
writePosition(position, dir);
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
if (raf != null)
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//获得url地址文件的总长度
public static long getlength(URL url) throws IOException {
long length = 0;
URLConnection con = url.openConnection();
length = con.getContentLength();
return length;
}
//读取记录文件中的记录点
public long readPosition(File dir) {
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File(dir, "len.txt"), "rw");
raf.seek(0);
return raf.readLong();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (raf != null)
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return 0L;
}
//写入记录文件记录点
public void writePosition(long position, File dir) {
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File(dir, "len.txt"), "rw");
raf.writeLong(position);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (raf != null)
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException,
URISyntaxException {
String str = "http://192.168.46.254:8888/easyBuy/images/plmm.jpg";
File dir = new File("C:\\Documents and Settings\\Administrator\\桌面");
File target = new File("C:\\Documents and Settings\\Administrator\\桌面",
str.substring(str.lastIndexOf("/")));
URL url = new URL(str);
for (int i = 0; i < 4; i++) {
new Download(str, dir, target, (i * getlength(url)) / 4,
((i + 1) * getlength(url)) / 4).start();
}
}
}
线程下载进度记录器
package com.softeem.download;
import java.text.NumberFormat;
public class ProgressListener extends Thread{
private long total;
private long current;
public void setCurrent(long current) {
this.current = current;
}
public ProgressListener(long total, long current) {
super();
this.total = total;
this.current = current;
}
@Override
public void run() {
while(total > current)
{
double pers = (double)current/total;
NumberFormat fmt = NumberFormat.getPercentInstance();
String s = fmt.format(pers);
System.out.println("当前进度:" + s);
try {
sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("当前进度:100%");
System.out.println("拷贝完成!");
}
}