web大文件下载+断点续传
?例如10M大小,使用3个线程来下载,
下载结束位置:(线程id+1)*每条线程下载的数据长度-1=?
Activity代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
int size = msg.getData().getInt("size");
int progress = (int) (temp * 100);
}
};
private static ProgressBar progressbar;
protected void onCreate(Bundle savedInstanceState) {
initView();
btn_download = (Button) findViewById(R.id.btn_download);
switch (v.getId()) {
download();
}
//下载文件
String path = Environment.getExternalStorageDirectory() + "/morethreaddownload/";
// 如果文件目录不存在,就创建此文件夹
// 要下载的文件路径
int threadcount = 5;
DownLoadTask task = new DownLoadTask(progressbar,urlstring,filepath,threadcount);
private void submit() {
if (TextUtils.isEmpty(urlstring)) {
}
// 网路文件的下载地址
private String filePath;
// 进度条
public static int filesize;
//构造方法
this.downloadUrl = downloadUrl;
}
@Override
// 线程数组
URL url = new URL(downloadUrl);
if (conn.getResponseCode() == 200) {
if (filesize <= 0) {
}
// 设置progressbar的最大值
// 计算每条线程下载的数据长度
File file = new File(filePath);
threads[i] = new FileDownloadThread(file,url,(i+1),blockSize);
}
// 下载的总长度
isfinished = true;
downloadAllsize += threads[i].getDownloadlenght();
}
Message message = new Message();
Thread.sleep(1000);
}
}
class FileDownloadThread extends Thread {
*/
* 文件下载路径 (文件网址)
/**
private int threadId;
*/
private boolean isCompleted;
public FileDownloadThread(File file, URL downloadUrl, int threadId, int blockSize) {
this.threadId = threadId;
super.run();
HttpURLConnection conn = (HttpURLConnection) downloadUrl.openConnection();
int startpos=blockSize*(threadId-1);
conn.setRequestProperty("Range","bytes="+startpos+"-"+endpos);
raf=new RandomAccessFile(file,"rwd");
while((len=bis.read(buffer,0,1024))!=-1){
}
e.printStackTrace();
//每个线程下载的数据长度
}
return isCompleted;
/**
private File file;
*/
* 当前下载线程ID
/**
private int blockSize;
this.file = file;
this.blockSize = blockSize;
@Override
BufferedInputStream bis=null;
RandomAccessFile raf=null;
conn.setAllowUserInteraction(true);
int endpos=blockSize*threadId-1;
byte[] buffer=new byte[1024];
raf.seek(startpos);
raf.write(buffer,0,len);
isCompleted=true;
}
public int getDownloadlenght() {
//返回线程是否下载完成
}