久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

用 Java 下載整個 FTP 目錄(Apache Net Commons)

Download entire FTP directory in Java (Apache Net Commons)(用 Java 下載整個 FTP 目錄(Apache Net Commons))
本文介紹了用 Java 下載整個 FTP 目錄(Apache Net Commons)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試遞歸遍歷登錄 FTP 服務器后到達的整個根目錄.

I am trying to recursively iterate through the entire root directory that I arrive at after login to the FTP server.

我能夠連接,我真正想做的就是遞歸整個結構,下載每個文件和文件夾,并使其具有與 FTP 上相同的結構.到目前為止,我所擁有的是一種有效的下載方法,它進入服務器并獲取我的整個文件結構,這非常棒,除了它在第一次嘗試時失敗,然后在第二次嘗試.我得到的錯誤如下:

I am able to connect, all I really want to do from there is recurse through the entire structure and and download each file and folder and have it in the same structure as it is on the FTP. What I have so far is a working download method, it goes to the server and gets my entire structure of files, which is brilliant, except it fails on the first attempt, then works the second time around. The error I get is as follows:

java.io.FileNotFoundException: 輸出目錄 est estFile.png(系統找不到指定的路徑)

java.io.FileNotFoundException: output-directory est estFile.png (The system cannot find the path specified)

我設法完成了本地目錄的上傳功能,但無法完全下載工作,經過多次嘗試,我真的需要一些幫助.

I managed to do upload functionality of a directory that I have locally, but can't quite get downloading to work, after numerous attempts I really need some help.

public static void download(String filename, String base)
{
    File basedir = new File(base);
    basedir.mkdirs();

    try
    {
        FTPFile[] ftpFiles = ftpClient.listFiles();
        for (FTPFile file : ftpFiles)
        {
            if (!file.getName().equals(".") && !file.getName().equals("..")) {
                // If Dealing with a directory, change to it and call the function again
                if (file.isDirectory())
                {
                    // Change working Directory to this directory.
                    ftpClient.changeWorkingDirectory(file.getName());
                    // Recursive call to this method.
                    download(ftpClient.printWorkingDirectory(), base);

                    // Create the directory locally - in the right place
                    File newDir = new File (base + "/" + ftpClient.printWorkingDirectory());
                    newDir.mkdirs();

                    // Come back out to the parent level.
                    ftpClient.changeToParentDirectory();
                }
                else
                {
                    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                    String remoteFile1 = ftpClient.printWorkingDirectory() + "/" + file.getName();
                    File downloadFile1 = new File(base + "/" + ftpClient.printWorkingDirectory() + "/" + file.getName());
                    OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
                    boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
                    outputStream1.close();
                }
            }
        }
    }
    catch(IOException ex)
    {
        System.out.println(ex);
    }
}

推薦答案

你的問題(嗯,在我們去掉 ... 之后你現在的問題和您解決了二進制問題)是您在調用 newDir.mkdirs() 之前正在執行遞歸步驟.

Your problem (well, your current problem after we got rid of the . and .. and you got past the binary issue) is that you are doing the recursion step before calling newDir.mkdirs().

假設你有一棵樹

.
..
someDir
   .
   ..
   someFile.txt
someOtherDir
   .
   ..
someOtherFile.png

你所做的是跳過點文件,看到 someDir 是一個目錄,然后立即進入其中,跳過它的點文件,然后查看 someFile.txt,并處理它.你還沒有在本地創建 someDir,所以你得到了一個異常.

What you do is skip the dot files, see that someDir is a directory, then immediately go inside it, skip its dot files, and see someFile.txt, and process it. You have not created someDir locally as yet, so you get an exception.

您的異常處理程序不會停止執行,因此控制會返回到遞歸的上層.此時它會創建目錄.

Your exception handler does not stop execution, so control goes back to the upper level of the recursion. At this point it creates the directory.

所以下次你運行你的程序時,本地的someDir目錄已經從之前的運行中創建好了,你看不出有什么問題.

So next time you run your program, the local someDir directory is already created from the previous run, and you see no problem.

基本上,您應該將代碼更改為:

Basically, you should change your code to:

            if (file.isDirectory())
            {
                // Change working Directory to this directory.
                ftpClient.changeWorkingDirectory(file.getName());

                // Create the directory locally - in the right place
                File newDir = new File (base + "/" + ftpClient.printWorkingDirectory());
                newDir.mkdirs();

                // Recursive call to this method.
                download(ftpClient.printWorkingDirectory(), base);

                // Come back out to the parent level.
                ftpClient.changeToParentDirectory();
            }

這篇關于用 Java 下載整個 FTP 目錄(Apache Net Commons)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 一级免费看片 | 国产成人高清视频 | 欧美久久视频 | 999国产精品视频 | 成人免费三级电影 | 中文字幕在线视频免费观看 | 免费久久久久久 | 精品久久久久一区 | 中文字幕一区二区三区日韩精品 | 懂色一区二区三区免费观看 | 中文字幕国产一区 | 欧美h视频 | 亚洲精品一区中文字幕乱码 | 亚洲欧美综合精品另类天天更新 | 夜夜草| 久久久久久久久精 | 黄色在线免费观看视频网站 | 国产va| 日韩理论电影在线观看 | 香蕉视频一区二区 | 国产九九九 | 日本在线视频一区二区 | 国产精品久久久久一区二区三区 | 日韩在线免费看 | 亚洲高清av在线 | 91精品国产日韩91久久久久久 | 谁有毛片 | 精品视频www | 亚洲欧美精 | 欧美13videosex性极品 | 日韩 欧美 综合 | 亚洲视频免费 | 一区二区三区免费观看 | 国产在线播 | 久久蜜桃av一区二区天堂 | 亚洲天堂精品一区 | 久久www免费人成看片高清 | 久久国内精品 | 一区二区三区精品视频 | 一区在线免费视频 | 超碰一区二区 |