您的位置:首页 > 移动开发 > Android开发

如何实现文件的断点续传,文件下载

2015-08-14 21:07 519 查看
import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.RandomAccessFile;

import java.net.HttpURLConnection;

import java.net.URL;

public class MultiDownLoad {

static int threadCount = 3;
static String path = "http://192.168.18.114:8080/QQPlayer.exe";
static int threadFinished = 0;
public static void main(String[] args) {

try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);

if(conn.getResponseCode() == 200){
//拿到要下载的文件的大小
int length = conn.getContentLength();

//指定临时文件的路径和文件名
File file = new File(getFileName(path));
//创建随机存储文件对象
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//设置临时文件的大小,跟服务器文件一模一样
raf.setLength(length);

//计算每个线程下载的字节数
int size = length / threadCount;

for (int i = 0; i < threadCount; i++) {
//计算3个线程下载数据的开始位置和结束位置
int startIndex = i * size;
int endIndex = (i + 1) * size - 1;
//如果是最后一个线程,那么结束位置特殊处理
if(i == threadCount - 1){
endIndex = length - 1;
}

// System.out.println("线程" + i + "的开始结束位置:" + startIndex + "---------" + endIndex);
//开始线程,传入线程id和下载的开始位置、结束位置
new DownLoadThread(i, startIndex, endIndex).start();
}

}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

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

}

class DownLoadThread extends Thread{
int threadId;
int startIndex;
int endIndex;

public DownLoadThread(int threadId, int startIndex, int endIndex) {
super();
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
}

@Override
public void run() {
try {
//设置记录下载进度的临时文件的路径和文件名
File fileProgress = new File(threadId + ".txt");
//判断保存下载进度的临时文件是否存在
if(fileProgress.exists()){
FileInputStream fis = new FileInputStream(fileProgress);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
//拿到临时文件中所保存的新的开始位置
int newStartIndex = Integer.parseInt(br.readLine());
startIndex = newStartIndex;

fis.close();
}
System.out.println("线程" + threadId + "的最终开始位置是:" + startIndex);
URL url = new URL(MultiDownLoad.path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
//设置请求的数据的范围                                         请求startIndex到endIndex个字节的数据
conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);

//建立连接
if(conn.getResponseCode() == 206){
InputStream is = conn.getInputStream();
int len = 0;
byte[] b = new byte[1024];
//当前线程下载的总字节数
int total = 0;

//指定临时文件的路径和文件名
File file = new File(MultiDownLoad.getFileName(MultiDownLoad.path));
//创建随机存储文件对象
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//设置线程从哪个位置开始写入数据到临时文件
raf.seek(startIndex);

//记录当前下载进度
int currentPosition = startIndex;

while((len = is.read(b)) != -1){
//把下载下来的源文件数据写入raf临时文件
raf.write(b, 0, len);

total += len;
RandomAccessFile rafProgress = new RandomAccessFile(fileProgress, "rwd");
currentPosition = startIndex + total;
//把下载进度写进rafProgress临时文件,下一次下载时,就以这个值作为新的startIndex
rafProgress.write((currentPosition + "").getBytes());
rafProgress.close();

// System.out.println("线程" + threadId + "已经下载" + total);
}
raf.close();

System.out.println("线程" + threadId + "下载完毕-----------------------");
MultiDownLoad.threadFinished++;

//如果这个条件成立,说明所有线程下载完毕
synchronized (MultiDownLoad.path) {
if(MultiDownLoad.threadFinished == MultiDownLoad.threadCount){
for (int i = 0; i < MultiDownLoad.threadCount; i++) {
File temp = new File(i + ".txt");
temp.delete();
}
MultiDownLoad.threadFinished = 0;
}
}

}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

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