📄 multidownload.java
字号:
package download;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.net.*;
public class MultiDownload {
boolean [] isStop;
Downloadsaction saction;
String url;
int byteCount;
long fileMark=0;
int threadNum;
boolean stopFlag;
DownloadGetFileLengh l=new DownloadGetFileLengh();
public void downProcess(String url,int byteCount,int threadNum)//start thread
{
for(int i=0;i<threadNum;i++)
{ int start=i*byteCount;
int end=byteCount*(i+1);
File file=new File("temp_file_"+i+".temp");
DownThread downThread=new DownThread(start,end,file,url,this,i);
new Thread(downThread).start();
}
}
public void uniteFile(int threadNum,File file) throws Exception
{ int i=0;
while(i<threadNum)
{File temp=new File("temp_file_" +i+".temp");
FileInputStream in=new FileInputStream(temp);
RandomAccessFile out=new RandomAccessFile(file,"rw");
byte [] bytes=new byte[1024];
int size=0;
out.seek(fileMark);
while((size=in.read(bytes,0,1024))>0)
{
out.write(bytes,0,size);
fileMark=fileMark+(long)size;
}
i++;
in.close();
out.close();
temp.delete();
}
}
public void downMulitFile(List downList) throws Exception
{
for(int k=0;k<downList.size();k++)
{
Downloadsaction saction=(Downloadsaction)downList.get(k);
int num=saction.getThreadNum();
File file=saction.getFileName();
String url=saction.getURL();
int length=l.getFileLength(url);
if(length!=-1)
{
final int byteNum=(int)length/num;
downProcess(url,byteNum,num);
isStop=new boolean[num];
boolean flag=l.allIsStop(isStop);
if(flag)
{
uniteFile(num,file);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -