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

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

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

問(wèn)題描述

我正在嘗試遞歸遍歷登錄 FTP 服務(wù)器后到達(dá)的整個(gè)根目錄.

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

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

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(系統(tǒng)找不到指定的路徑)

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

我設(shè)法完成了本地目錄的上傳功能,但無(wú)法完全下載工作,經(jīng)過(guò)多次嘗試,我真的需要一些幫助.

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

推薦答案

你的問(wèn)題(嗯,在我們?nèi)サ?... 之后你現(xiàn)在的問(wèn)題和您解決了二進(jìn)制問(wèn)題)是您在調(diào)用 newDir.mkdirs() 之前正在執(zhí)行遞歸步驟.

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().

假設(shè)你有一棵樹(shù)

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

你所做的是跳過(guò)點(diǎn)文件,看到 someDir 是一個(gè)目錄,然后立即進(jìn)入其中,跳過(guò)它的點(diǎn)文件,然后查看 someFile.txt,并處理它.你還沒(méi)有在本地創(chuàng)建 someDir,所以你得到了一個(gè)異常.

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.

您的異常處理程序不會(huì)停止執(zhí)行,因此控制會(huì)返回到遞歸的上層.此時(shí)它會(huì)創(chuàng)建目錄.

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.

所以下次你運(yùn)行你的程序時(shí),本地的someDir目錄已經(jīng)從之前的運(yùn)行中創(chuàng)建好了,你看不出有什么問(wèn)題.

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

基本上,您應(yīng)該將代碼更改為:

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

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

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

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 欧美专区在线 | 黄色小视频大全 | 中文字幕第二区 | 曰韩一二三区 | 欧美国产日韩精品 | 91夜色在线观看 | 国产人成在线观看 | 久久性色 | 在线观看国产视频 | 欧美性视频在线播放 | 亚洲国产精品va在线看黑人 | 精品美女久久久 | 91美女在线观看 | 成人妇女免费播放久久久 | 久久综合婷婷 | 日本三级线观看 视频 | 成人免费视频网站在线观看 | 一区二区三区视频在线 | 91久操视频 | 一区二区三区四区av | 亚洲精品久久久久久一区二区 | 亚洲国产aⅴ成人精品无吗 综合国产在线 | 美女操网站 | 国产乱码精品一区二区三区忘忧草 | 五月天天色 | 中文字幕第十一页 | 亚洲精品视频在线播放 | 日本午夜精品一区二区三区 | 久www| 久久精品国产一区 | 日韩精品成人av | 中文精品一区二区 | 一区二区激情 | 久久久久久a | 国精产品一区一区三区免费完 | 国产视频第一页 | 国产精品色综合 | 久久久久免费观看 | 91亚洲国产 | 999国产精品视频免费 | 福利一区在线观看 |