有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java下载目录中的所有文件和文件夹

我正试图从这个目录下载所有文件。然而,我只能让它作为一个文件下载url。我能做什么?我试着搜索这个问题,结果很混乱,人们开始建议使用httpclients。谢谢你的帮助,这是我目前的代码。有人建议我使用输入流来获取目录中的所有文件。那会进入一个数组吗?我试过这里的教程,但它没有帮助我理解

//ProgressBar/Install
            String URL_LOCATION = "http://www.futureretrogaming.tk/gamefiles/ProfessorPhys/";
            String LOCAL_FILE = filelocation.getText() + "\\ProfessorPhys\\";
            try {
                java.net.URL url = new URL(URL_LOCATION);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
                connection.addRequestProperty("User-Agent", "Mozilla/4.76"); 
                //URLConnection connection = url.openConnection();
                BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
                int available = stream.available();
                byte b[]= new byte[available];
                stream.read(b);
                File file = new File(LOCAL_FILE);
                OutputStream out  = new FileOutputStream(file);
                out.write(b);
            } catch (Exception e) {
                System.err.println(e);
            }

我还发现了这段代码,它将返回要下载的文件列表。有人能帮我把这两种代码结合起来吗

public class GetAllFilesInDirectory {

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

    File dir = new File("dir");

    System.out.println("Getting all files in " + dir.getCanonicalPath() + " including those in subdirectories");
    List<File> files = (List<File>) FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
    for (File file : files) {
        System.out.println("file: " + file.getCanonicalPath());
    }

}

}


共 (1) 个答案

  1. # 1 楼答案

    你需要下载页面,这是目录列表,解析它,然后下载页面中链接的inidiviudal文件

    你可以做一些像

    URL url = new URL("http:www.futureretrogaming.tk/gamefiles/ProfessorPhys");
    InputStream is = null;
    try {
        is = url.openStream();
        byte[] buffer = new byte[1024];
        int bytesRead = -1;
        StringBuilder page = new StringBuilder(1024);
        while ((bytesRead = is.read(buffer)) != -1) {
            page.append(new String(buffer, 0, bytesRead));
        }
        // Spend the rest of your life using String methods
        // to parse the result...
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (Exception e) {
        }
    }
    

    或者,你可以下载Jsoup并用它来做所有的艰苦工作

    try {
        Document doc = Jsoup.connect("http:www.futureretrogaming.tk/gamefiles/ProfessorPhys").get();
        Elements links = doc.getElementsByTag("a");
        for (Element link : links) {
            System.out.println(link.attr("href") + " - " + link.text());
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    

    输出

    ?C=N;O=D - Name
    ?C=M;O=A - Last modified
    ?C=S;O=A - Size
    ?C=D;O=A - Description
    /gamefiles/ - Parent Directory
    Assembly-CSharp-Editor-firstpass-vs.csproj - Assembly-CSharp-Edit..>
    Assembly-CSharp-Editor-firstpass.csproj - Assembly-CSharp-Edit..>
    Assembly-CSharp-Editor-firstpass.pidb - Assembly-CSharp-Edit..>
    Assembly-CSharp-firstpass-vs.csproj - Assembly-CSharp-firs..>
    Assembly-CSharp-firstpass.csproj - Assembly-CSharp-firs..>
    Assembly-CSharp-firstpass.pidb - Assembly-CSharp-firs..>
    Assembly-CSharp-vs.csproj - Assembly-CSharp-vs.c..>
    Assembly-CSharp.csproj - Assembly-CSharp.csproj
    Assembly-CSharp.pidb - Assembly-CSharp.pidb
    Assembly-UnityScript-Editor-firstpass-vs.unityproj - Assembly-UnityScript..>
    Assembly-UnityScript-Editor-firstpass.pidb - Assembly-UnityScript..>
    Assembly-UnityScript-Editor-firstpass.unityproj - Assembly-UnityScript..>
    Assembly-UnityScript-firstpass-vs.unityproj - Assembly-UnityScript..>
    Assembly-UnityScript-firstpass.pidb - Assembly-UnityScript..>
    Assembly-UnityScript-firstpass.unityproj - Assembly-UnityScript..>
    Assembly-UnityScript-vs.unityproj - Assembly-UnityScript..>
    Assembly-UnityScript.pidb - Assembly-UnityScript..>
    Assembly-UnityScript.unityproj - Assembly-UnityScript..>
    Assets/ - Assets/
    Library/ - Library/
    Professor%20Phys-csharp.sln - Professor Phys-cshar..>
    Professor%20Phys.exe - Professor Phys.exe
    Professor%20Phys.sln - Professor Phys.sln
    Professor%20Phys.userprefs - Professor Phys.userp..>
    Professor%20Phys_Data/ - Professor Phys_Data/
    Script.doc - Script.doc
    ~$Script.doc - ~$Script.doc
    ~WRL0392.tmp - ~WRL0392.tmp
    ~WRL1966.tmp - ~WRL1966.tmp
    

    然后,你需要为每个文件建立一个新的URL,并像之前那样读取

    例如,Assembly-CSharp-Edit..>hrefAssembly-CSharp-Editor-firstpass-vs.csproj,这似乎是一个相对链接,因此您需要在它前面加上http://www.futureretrogaming.tk/gamefiles/ProfessorPhys来创建http://www.futureretrogaming.tk/gamefiles/ProfessorPhys/Assembly-CSharp-Editor-firstpass-vs.csproj的新URL

    你需要为你想要抓取的每一个元素都这样做