编写程序下载reactos所有开源代码
http://svn.reactos.org/下目录不能直接用svn下载,svn工具只可下载其下某个子目录的文件夹,这是因为第一个链接放的是根目录,因此构成了循环结构,svn工具会报错,但是看着里面的结构想把所有资源都down下来,只好自己写了一个java程序:150行 五易其稿以铸神器。我用的tortoise svn,用svn工具多了,就会发现经常会有奇怪的错误,什么sql啦,lock啦,种种怪事情,自己写就okpackage resolve;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class resolve
{
public static String url="http://svn.reactos.org/";
public static String path="k:/reactos/";
public static int filethreadnum=0;
public static class FileThread extends Thread
{
String filepath;
String curnode;
FileThread(String filepath,String curnode)
{
this.filepath=filepath;
this.curnode=curnode;
}
public void run()
{
try
{
while(filethreadnum>36)
{
sleep(1000);
}
filethreadnum++;
int byteread=0;
int bytesum=0;
URL weburl=new URL(url+filepath+curnode);
URLConnection con=weburl.openConnection();
InputStream instream=con.getInputStream();
FileOutputStream fs=new FileOutputStream((path+filepath+curnode).replace("%20"," "));
byte[] buffer=new byte;
while((byteread=instream.read(buffer)) != -1)
{
bytesum+=byteread;
fs.write(buffer,0,byteread);
System.out.println("\t\t当前下载文件:"+filepath+curnode+"\t当前大小:"+bytesum);
}
fs.close();
instream.close();
filethreadnum--;
}
catch(Exception e)
{
System.out.println("error");
}
}
}
public static String createFolder(String folderPath)
{
String txt = folderPath;
try
{
File myFilePath = new File(txt);
txt = folderPath;
if (!myFilePath.exists())
{
myFilePath.mkdir();
}
}
catch (Exception e)
{
System.out.println("错误!");
}
return txt;
}
public static void myresolve(Element e,String filepath) throws IOException
{
try
{
String curnode=e.attr("href");
if(!curnode.equals("../") && !curnode.equals("svn/"))
{//非父目录
if(curnode.charAt(curnode.length()-1) == '/')
{//目录
createFolder((path+filepath+curnode).replace("%20"," "));
Document doc=Jsoup.connect(url+filepath+curnode).timeout(0).get();
System.out.println("当前目录:"+url+filepath+curnode);
Elements items=doc.select("tbody tr a");
for(Element ele1:items)
{
myresolve(ele1,filepath+curnode);
}
items.clear();
items=doc.select("ul li a");
for(Element ele2:items)
{
myresolve(ele2,filepath+curnode);
}
}
else
{//文件
File curfile=new File((path+filepath+curnode).replace("%20"," "));
if(curfile.exists())
return;
while(filethreadnum>36)
{
Thread.sleep(1000);
}
(new FileThread(filepath,curnode)).start();
}
}
}
catch(Exception exc)
{
System.out.println("错误!");
}
}
public static void main(String[] args) throws IOException
{
try
{
Document doc = Jsoup.connect(url).timeout(0).get();
Elements items=doc.select("tbody tr a");
createFolder(path);
for(Element e1:items)
{
myresolve(e1,"");
}
items=doc.select("ul li a");
for(Element e2:items)
{
myresolve(e2,"");
}
while(filethreadnum != 0)
{
Thread.sleep(1000);
}
}
catch(Exception exc)
{
System.out.println("错误!");
}
}
} 多谢分享 顶.........
页:
[1]