📄 multidownload.java~4~
字号:
package com;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2009</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import java.util.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;
public class MultiDownload {
int i=0;
int fileMark = 0;
DownloadUtil downUtil = new DownloadUtil();
boolean isStop[];
public void downProcess(String url,int byteCount,int threadNum) throws Exception{
while(i<threadNum){
final int startPosition = byteCount*i;
final int endPosition = byteCount*(i+1);
File file = new File("temp_file_"+i+".temp");
DownThread fileThread = new DownThread(url,file,
startPosition,endPosition,this,i);
new Thread(fileThread).start();
i++;
}
}
public void downMulitFile(DownloadInfo downInfo){
try{
String url = downInfo.getUrl();
int threadNum = downInfo.getThreadNum();
String filename = downInfo.getFilename();
int fileLength = downUtil.getFileLength(url);
isStop = new boolean[downInfo.getThreadNum()];
if (fileLength != -1) {
final int byteCount = (int) (fileLength / threadNum) + 1;
boolean stopFlag = true;
downProcess(url, byteCount, threadNum);
stopFlag = downUtil.isFinished(isStop);
if (stopFlag) {
File file = new File(filename);
uniteFile(threadNum, file);
downInfo.setIsFinished(true);
}
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
public void uniteFile(int threadNum,File file) throws Exception{
for(int i=0;i<threadNum;i++){
File tempfile = new File("temp_file_"+i+".temp");
FileInputStream fis = new FileInputStream(tempfile);
RandomAccessFile raf = new RandomAccessFile(file, "rw");
byte[] buf = new byte[1024];
int len = -1;
raf.seek((long) fileMark);
while ((len = fis.read(buf)) != -1) {
raf.write(buf, 0, len);
fileMark += len;
}
fis.close();
raf.close();
tempfile.delete();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -