这个项目其实已经在进行,我的最终目标是写一个搜索引擎一类的东西,不过作为一个连JAVA是什么都不知道的人,我选择以加强版的NetAnts作为往上提升的基石.
其实算法部份我已经写了一半,所以把东西搬出来,请有兴趣的朋友一起来看看。现在我写了四个类
1:PositionManager类,是用来为每个线程分配下载任务的。
2: DownloadTask类,PositionManager所分配的每个任务都会以DownloadTask作为载体传给下载线程
3: GetFile类,这个类就是对外的接口,所要下载文件的位置和保存的位置都是传给它,它还负责管理下载线程。(这个类我不知道怎么写,我到底以怎么样的方法去管理(增加和停止)正在运行的下载线程)
4:Download类,这个类是空的,我还想不到怎么样实现,我是想它负责简下载的。我把每个类的源码写出来,请大家给点意见,请各位前辈百忙中抽中指点,请各朋友同一层次的朋友也帮忙想想。
我叫大勇,QQ:23281396,请注明csdn.net
/*
* 创建日期 2006-2-1
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import java.util.*;
public class PositionManager {
/**
* if file is very large,maxThreads will be set to 10
* if threadsCount == maxThreads
* nobody can take new threads
*/
private int maxThreads = 5;
private int threadsCount = 0;
/**
* bufferTimes help to detect does file is large
*/
private int bufferTimes = 0;
/**
* use treeSet can autosort, but maybe it's not necessary
*/
private SortedSet<DownloadTask> alreadyDown_ts = new TreeSet<DownloadTask>();
public PositionManager(long fileLength, long bufferSize){
this.bufferTimes = (int)(fileLength/bufferSize);
if(bufferTimes > 100) maxThreads = 10;
DownloadTask t = new DownloadTask();
t.startPosition = 0;
t.endPosition = (fileLength /10);
t.finishTag = false;
alreadyDown_ts.add(t);
for(int i=1; i<10; i++){
DownloadTask t2 = new DownloadTask();
if(i < 9){
t2.startPosition = (((fileLength/10) * i) + 1);
t2.endPosition = (fileLength / 10) * (i + 1);
t2.finishTag = false;
alreadyDown_ts.add(t2);
}else{
t2.startPosition = (((fileLength/10) * 9) + 1);
t2.endPosition = fileLength;
t2.finishTag = false;
alreadyDown_ts.add(t2);
System.out.println(alreadyDown_ts);
}
}
}
/**
* when the thread had been stoped
* or finish,use this to report result
* @param t use this to pass the result
*/
public synchronized void alreadyDown(DownloadTask t){
//if find out same startPosition, store it,change finishTag
//and use the unfinish task creat new task,and store the new task
//if can't find out same startPosition, just simply ignore it
for(DownloadTask i : alreadyDown_ts){
if( i.startPosition == t.startPosition){
//use t2 to create unfinish task
DownloadTask t2 = new DownloadTask();
if(i.endPosition == t.endPosition){
i.finishTag = true;
break;
}else if(i.endPosition > t.endPosition){
t2.startPosition = t.endPosition + 1;
t2.endPosition = i.endPosition;
t2.finishTag = false;
alreadyDown_ts.add(t2);
i.endPosition = t.endPosition;
i.finishTag = true;
break;
}
}
}
}
/**
* use the DownloadTask object to pass the
* startPosition and endPosition
* @param D
* @return return true if success get task
*/
public synchronized boolean getTask(DownloadTask d){
if(threadsCount >= maxThreads) return false;
for(DownloadTask i : alreadyDown_ts){
if( !(i.finishTag) ){
i.finishTag = true;
d.startPosition = i.startPosition;
d.endPosition = i.endPosition;
d.finishTag = i.finishTag;
return true;
}
}
return false;
}
public static void main(String[] args){
PositionManager pm = new PositionManager(101, 10);
//boolean result = false;
DownloadTask t = new DownloadTask();
pm.getTask(t);
System.out.println(t);
pm.getTask(t);
System.out.println(t);
t.endPosition = 15;
pm.alreadyDown(t);
pm.getTask(t);
System.out.println(t);
}
}
//:~
/*
* 创建日期 2006-2-1
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import java.lang.Comparable;
public class DownloadTask implements Comparable {
public long startPosition = 0;
public long endPosition = 0;
public boolean finishTag = false;
public String toString(){
return "startPosition is: " + startPosition + "\t" +
"endPosition is: " + endPosition + "\t"
+"finishTag is: " + finishTag + "\n";
} public int compareTo(Object arg0) {
if (this.startPosition < ((DownloadTask)arg0).startPosition) {
return -1;
}else if( this.startPosition == ((DownloadTask)arg0).startPosition ){
return 0;
}else return 1;
// TODO 自动生成方法存根
}
}
//:~
其实算法部份我已经写了一半,所以把东西搬出来,请有兴趣的朋友一起来看看。现在我写了四个类
1:PositionManager类,是用来为每个线程分配下载任务的。
2: DownloadTask类,PositionManager所分配的每个任务都会以DownloadTask作为载体传给下载线程
3: GetFile类,这个类就是对外的接口,所要下载文件的位置和保存的位置都是传给它,它还负责管理下载线程。(这个类我不知道怎么写,我到底以怎么样的方法去管理(增加和停止)正在运行的下载线程)
4:Download类,这个类是空的,我还想不到怎么样实现,我是想它负责简下载的。我把每个类的源码写出来,请大家给点意见,请各位前辈百忙中抽中指点,请各朋友同一层次的朋友也帮忙想想。
我叫大勇,QQ:23281396,请注明csdn.net
/*
* 创建日期 2006-2-1
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import java.util.*;
public class PositionManager {
/**
* if file is very large,maxThreads will be set to 10
* if threadsCount == maxThreads
* nobody can take new threads
*/
private int maxThreads = 5;
private int threadsCount = 0;
/**
* bufferTimes help to detect does file is large
*/
private int bufferTimes = 0;
/**
* use treeSet can autosort, but maybe it's not necessary
*/
private SortedSet<DownloadTask> alreadyDown_ts = new TreeSet<DownloadTask>();
public PositionManager(long fileLength, long bufferSize){
this.bufferTimes = (int)(fileLength/bufferSize);
if(bufferTimes > 100) maxThreads = 10;
DownloadTask t = new DownloadTask();
t.startPosition = 0;
t.endPosition = (fileLength /10);
t.finishTag = false;
alreadyDown_ts.add(t);
for(int i=1; i<10; i++){
DownloadTask t2 = new DownloadTask();
if(i < 9){
t2.startPosition = (((fileLength/10) * i) + 1);
t2.endPosition = (fileLength / 10) * (i + 1);
t2.finishTag = false;
alreadyDown_ts.add(t2);
}else{
t2.startPosition = (((fileLength/10) * 9) + 1);
t2.endPosition = fileLength;
t2.finishTag = false;
alreadyDown_ts.add(t2);
System.out.println(alreadyDown_ts);
}
}
}
/**
* when the thread had been stoped
* or finish,use this to report result
* @param t use this to pass the result
*/
public synchronized void alreadyDown(DownloadTask t){
//if find out same startPosition, store it,change finishTag
//and use the unfinish task creat new task,and store the new task
//if can't find out same startPosition, just simply ignore it
for(DownloadTask i : alreadyDown_ts){
if( i.startPosition == t.startPosition){
//use t2 to create unfinish task
DownloadTask t2 = new DownloadTask();
if(i.endPosition == t.endPosition){
i.finishTag = true;
break;
}else if(i.endPosition > t.endPosition){
t2.startPosition = t.endPosition + 1;
t2.endPosition = i.endPosition;
t2.finishTag = false;
alreadyDown_ts.add(t2);
i.endPosition = t.endPosition;
i.finishTag = true;
break;
}
}
}
}
/**
* use the DownloadTask object to pass the
* startPosition and endPosition
* @param D
* @return return true if success get task
*/
public synchronized boolean getTask(DownloadTask d){
if(threadsCount >= maxThreads) return false;
for(DownloadTask i : alreadyDown_ts){
if( !(i.finishTag) ){
i.finishTag = true;
d.startPosition = i.startPosition;
d.endPosition = i.endPosition;
d.finishTag = i.finishTag;
return true;
}
}
return false;
}
public static void main(String[] args){
PositionManager pm = new PositionManager(101, 10);
//boolean result = false;
DownloadTask t = new DownloadTask();
pm.getTask(t);
System.out.println(t);
pm.getTask(t);
System.out.println(t);
t.endPosition = 15;
pm.alreadyDown(t);
pm.getTask(t);
System.out.println(t);
}
}
//:~
/*
* 创建日期 2006-2-1
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import java.lang.Comparable;
public class DownloadTask implements Comparable {
public long startPosition = 0;
public long endPosition = 0;
public boolean finishTag = false;
public String toString(){
return "startPosition is: " + startPosition + "\t" +
"endPosition is: " + endPosition + "\t"
+"finishTag is: " + finishTag + "\n";
} public int compareTo(Object arg0) {
if (this.startPosition < ((DownloadTask)arg0).startPosition) {
return -1;
}else if( this.startPosition == ((DownloadTask)arg0).startPosition ){
return 0;
}else return 1;
// TODO 自动生成方法存根
}
}
//:~
解决方案 »
- 额,这个程序在DOS下编译会发出“滴滴滴滴”的定时炸弹的声音,求高手解答!
- java 类中的一点小问题
- 求一个poi中eventusermodel方式读取Excel文档内容的例子
- 如何把系统时间转换成unix时间
- 新学java,第一个程序就被Jcreator郁闷到了,请您帮忙!
- java怎么获取前三个月的第一天的日期
- 哪里有java的examples(源代码)和java的类库的说明?
- 高薪诚聘java高手。
- 关于JAVA的简单问题(送分)
- 为什么我的JDBC创建new URL,driver选择oracle,URL不让我选择OCI?
- 紧急求助文件操作的问题!
- 触发文件中的"打开"和"保存"的事件,不能写在匿名类中吗?
/*
* 创建日期 2006-1-28
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/import java.io.*;
import java.net.*;
/**
* @author GDY
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/public class GetFile implements Runnable { /* (非 Javadoc)
* @see java.lang.Runnable#run()
*/
private URL u = null;
private URLConnection uc = null;
private InputStream is = null;
private OutputStream os = null;
public static final long FileNotFound = 0;
public static final long FileSizeUnknow = 1;
private long bufferSize = 0;
private int totalThreads = 0;
private String saveTo = null;
/**
* use PipedWriter to communicate between functions and run
*/
private PipedWriter out = new PipedWriter();
public PipedWriter getPipedWriter() { return out; } /**
* check the file and get file size
* return PositionManager.FileNotFound
* return PositionManager.FileSizeUnknow
* @return
*/
private long intactCheck(URL u){
/**
* return this
*/
long result = 0;
/**
* mak a HttpURLconnection to connect server
*
*/
HttpURLConnection uc = null;
/**
* get the InputStream to server
*/
InputStream is = null;
try{
uc = (HttpURLConnection)u.openConnection();
int Response = uc.getResponseCode();;
is = uc.getInputStream();
result = uc.getContentLength();
if( Response >= 400){
result = FileNotFound;
}else if( result == -1 ){
result = FileSizeUnknow;
} }catch(IOException e){
result = FileNotFound;
}finally{
try{
is.close();
uc.disconnect();
}catch(IOException e){}
}
return result;
}
/*
* Constructor GetFile
* @param fileAddress where the file can be download
* @param saveTo where the file should be saveTo
*/
public GetFile(String fileAddress, String saveTo,int totalThreads){
try{
this.u = new URL(fileAddress);
this.uc = u.openConnection();
this.is = uc.getInputStream();
this.os = new FileOutputStream(saveTo);
this.totalThreads = totalThreads;
this.saveTo = saveTo;
}catch(Exception e){
e.printStackTrace();
}
}
public void run() {
long result = intactCheck(u);
PositionManager pm = null;
Download[] downloadArray = new Download[10];
if(result == FileNotFound){
System.out.println("FileNotFound");
}else if(result == FileSizeUnknow){
System.out.println("FileSizeUnknow");
}else{
pm = new PositionManager(result,bufferSize);
for(int i=0; i<totalThreads; i++){
DownloadTask t = new DownloadTask();
if(pm.getTask(t)){
downloadArray[i] = new Download(t,saveTo);
}
}
for(Download t : downloadArray) t.start();
PipedReader in;
try{
in = new PipedReader(this.getPipedWriter());
}catch(IOException ioe){
ioe.printStackTrace();
} while(true){
}
}
/*// TODO 自动生成方法存根
try{
byte[] inputBuffer = new byte[1024];
//int availableByte = is.available();
int readByte = 0;
while(true){
readByte = is.read(inputBuffer);
if (readByte == -1) break;
os.write(inputBuffer,0,readByte);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
is.close();
os.close();
}catch(Exception e){
e.printStackTrace();
}
}*/
} /*public static void main(String[] args) {
if (args.length != 2) System.exit(-1);
GetFile gf = new GetFile(args[0], args[1]);
Thread t = new Thread(gf);
System.out.println("Started Download \"" + args[0] + "\" To " + args[1]);
t.start();
System.out.println("Download \"" + args[0] + "\" finish");
}
*/
}
//:~
/*
* 创建日期 2006-2-4
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*//**
* @author GDY
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import java.lang.Thread;
public class Download extends Thread{
public Download(DownloadTask t, String saveTo){
System.out.println("ok");
} public void run() {
// TODO 自动生成方法存根
}}
一开始就确定下载的线程很容易,但如果正在下的时候停止现有的线程要怎么做呢?