断点续传的代码
来源:互联网 发布:软件出现乱码怎么办 编辑:程序博客网 时间:2024/06/08 21:07
注意事项:每次写流的时候,记得关掉,否则会出现,写到后面的流使用的文件等东东会出现删不掉的情况,影响用户的体验
publicclassMultidload {
staticintThreadCount= 3;
staticintfinishedThread= 0;
//确定下载地址
staticStringpath="http://192.168.6.125:8080/TakeColor.exe";
publicstaticvoidmain(String[] args){
//发送get请求,请求这个资源的地址
try{
URL url =newURL(path);
HttpURLConnection conn =(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
if(conn.getResponseCode()==200){
//拿到所请求文件的大小
intlength = conn.getContentLength();
File file =newFile("TakeColor.exe");
//生存临时文件
RandomAccessFile raf =newRandomAccessFile(file,"rwd");
//设置临时文件的大小
raf.setLength(length);
raf.close();
//计算出每个线程应该下载多少字节
intsize =length /ThreadCount;
for(inti =0;i<ThreadCount;i++){
//计算线程开始和结束的下载位置
intstartIndex = i * size;
intendIndex = (i + 1) * size - 1;
//如果是最后一个线程,那么结束的位置写死
if(i==ThreadCount-1){
endIndex = length-1;
}
System.out.println("线程"+ i +"的下载区间是:"+startIndex +"---"+ endIndex);
newDownLoadThread(startIndex,endIndex,i).start();
}
}
}catch(Exception e) {
//TODOAuto-generated catch block
e.printStackTrace();
}
}
}
classDownLoadThreadextendsThread{
intstartIndex;
intendIndex;
intthreadId;
publicDownLoadThread(intstartIndex,intendIndex,intthreadId){
super();
this.startIndex=startIndex;
this.endIndex=endIndex;
this.threadId=threadId;
}
publicvoidrun() {
//TODOAuto-generated method stub
//再次发送http请求,下载原文件
HttpURLConnection conn;
try{
File progressFile =newFile(threadId+".text");
//判断进度临时问价是否存在
if(progressFile.exists()){
FileInputStream fis =newFileInputStream(progressFile);
BufferedReader br =newBufferedReader(newInputStreamReader(fis));
//从进度临时文件中读取上一次临时文件下载的总进度,然后与原本的开始位置相加,得到新的开始位置
startIndex+=Integer.parseInt(br.readLine()) ;
fis.close();
}
URL url =newURL(Multidload.path);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
//设置本次http请求所请求的数据的区间
conn.setRequestProperty("Range","bytes="+startIndex+"-"+endIndex);
//请求部分数据,响应码是206
if(conn.getResponseCode()==206){
//流里此时只有1/3原文件的数据
InputStream is = conn.getInputStream();
byte[] b=newbyte[1024];
intlen =0;
inttotal=0;
//拿到临时文件的输出流
File file =newFile("TakeColor.exe");
//生存临时文件
RandomAccessFile raf =newRandomAccessFile(file,"rwd");
//把文件的写入位置移动至startIndex
raf.seek(startIndex);
while((len = is.read(b))!=-1){
raf.write(b, 0, len);
total +=len;
System.out.println("线程"+threadId+"下载了"+total);
//生成一个专门用来记录下载进度的临时文件
RandomAccessFile progressRaf =newRandomAccessFile(progressFile,"rwd");
//每次读取流里的数据之后,同步把当前线程的总进度写进临时文件中
progressRaf.write((total+"").getBytes());
progressRaf.close();
}
raf.close();
Multidload.finishedThread++;
synchronized(Multidload.path) {
if(Multidload.finishedThread==Multidload.ThreadCount){
for(inti= 0; i < Multidload.ThreadCount;i++) {
File f =newFile(i+ ".text");
f.delete();
}
Multidload.finishedThread= 0;
}
}
}
}catch(IOException e) {
//TODOAuto-generated catch block
e.printStackTrace();
}
}
}
0 0
- 一个断点续传的代码
- 断点续传的代码
- C# 关于断点续传的代码资料1
- S60 3rd断点续传的代码发布
- C# 关于断点续传的代码资料1
- android断点续传代码
- Android断点续传核心代码
- Android断点续传核心代码
- 断点续传核心代码
- Java断点续传服务器代码
- java断点续传代码
- Android断点续传核心代码
- Java断点续传服务器代码
- 断点续传核心代码
- java断点续传代码
- 用Java实现断点续传的基本思路和代码
- 代码笔记 | PHP支持断点续传,分块下载的类
- JAVA断点续传实现方法代码
- 关于Android v7包的资源文件错误问题解决方案
- 网络下载,配上进度条
- java web面试
- 将多线程下载和断点续传,移值到手机上
- 加密算法比较3DES AES RSA ECC MD5 SHA1等
- 断点续传的代码
- 【ORA-01455】转换列溢出整数数据类型
- 多线程下载的写法,java版本
- jQuery插件之lightBox图片灯箱
- HttpClient框架发送get和post网络请求
- PHP正则表达式收集
- 从服务器打开查看HTML类型文件
- 多线程--线程同步、死锁、守护线程、多线程下载
- Spring Boot使用Druid和监控配置