2016-07-01 5 views
0

"localhost:8888/fileserver"に静的ファイルサーバーがあります。 サーバーからファイルをダウンロードするために、javaでプログラムを作成しようとしています。ファイルサーバーは3つのフォルダで構成されているため、ディレクトリを自動的に通過してコンピュータにコピーするスクリプトを作成しようとしています。Javaを使用してオンラインファイルディレクトリ(localhost)をダウンロードするには

これを再帰的に実行するlinuxのwget関数があります。 Javaでこれを行う方法はありますか? これをやり直すか、進めるべきかアドバイスできますか?

は以下

答えて

0

は、オンラインディレクトリを経由してダウンロードするために必要なすべてのリンクをバック返すためのコードです、ありがとうございました。

その後、私は個々のリンクをダウンロードするだけです。

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

public class WebCrawler { 
    //Created a global list variable to save the links 
    static List<String> createList = new ArrayList<String>(); 

    public static void main(String[] args) throws IOException { 

     String url = "http://localhost:8888"; 
     System.out.println(myCrawler(url)+"\n"+"Size: "+myCrawler(url).size()); 

    }  

    public static List<String> myCrawler(String url) throws IOException{ 
     //Creates an open connection to a link 
     Document doc = Jsoup.connect(url).ignoreContentType(true).get(); 
     Elements links = doc.select("a[href]"); 

     //Recursively iterates through all the links provided on the initial url 
     for (Element i : links) { 
      String link = print("%s", i.attr("abs:href")); 
      if (link.endsWith("/")){myCrawler(link);} //Recursive part, calls back on itself 
      else {createList.add(link);} 
     } 
     return createList; 
    } 

    //Translates the link into a readable string object 
    private static String print(String msg, Object... args){return String.format(msg, args);} 
} 
関連する問題