繁体   English   中英

如何使用Java下载在线文件目录(localhost)

[英]How to download an online file directory (localhost) using Java

我在“ localhost:8888 / fileserver”处有一个静态文件服务器。 我正在尝试用Java编写程序以从服务器下载文件。 文件服务器由三个文件夹组成,因此我试图编写一个脚本,该脚本会自动通过目录并将其复制到我的计算机上。

我知道有一个针对Linux的wget函数可以递归地完成此任务。 有没有办法用Java做到这一点? 请您就我应该怎么做或继续进行工作提出建议。

谢谢

以下是浏览在线目录并返回所有下载所需链接的代码。

之后,我只需要下载每个单独的链接。

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);}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM