Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Ftp下载的文件大小为0 KB_Java_Ftp - Fatal编程技术网

Java Ftp下载的文件大小为0 KB

Java Ftp下载的文件大小为0 KB,java,ftp,Java,Ftp,我正在尝试将FTP路径中的所有文件下载到本地系统。但下载的文件大小为0 KB。问题是什么。请帮我找到错误。(文件类型为*.zip、*.pdf、*.xml) 提前谢谢 package ConnectFTP; import java.io.FileOutputStream; import java.io.IOException; import java.util.Calendar; import java.util.logging.Level; import java.util.logging.L

我正在尝试将FTP路径中的所有文件下载到本地系统。但下载的文件大小为0 KB。问题是什么。请帮我找到错误。(文件类型为*.zip、*.pdf、*.xml)

提前谢谢

package ConnectFTP;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

public class FTPDownloader {

FTPClient ftp = null;

public FTPDownloader(String host, String user, String pwd) throws Exception {
    ftp = new FTPClient();
    int reply;
    ftp.connect(host);
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        throw new Exception("Exception in connecting to FTP Server");
    }
    ftp.login(user, pwd);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    ftp.enterLocalPassiveMode();
}

public void downloadFile(String remoteFilePath, String localFilePath) {
    try (FileOutputStream fos = new FileOutputStream(localFilePath)) {
        this.ftp.retrieveFile(remoteFilePath, fos);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}
public void disconnect() {
    if (this.ftp.isConnected()) {
        try {
            this.ftp.logout();
            this.ftp.disconnect();
        } catch (IOException ioe) {

        }
    }
}

public static void main(String[] args) {
    try {
        FTPDownloader ftpDownloader =
                new FTPDownloader("192.168.3.1", "Adminuser", "password");
        ftpDownloader.listFolders();
        System.out.println("File downloaded successfully");
        ftpDownloader.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void listFolders() {
    try {
        String ftpBasePath = "dev";
        FTPFile[] fileList = ftp.listDirectories("/" + ftpBasePath);
        for (FTPFile file : fileList) {
            listFiles(file.getName());
        }
    } catch (IOException ex) {
        Logger.getLogger(FTPDownloader.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public void listFiles(String path) {
    try {
        FTPFile[] fileList = ftp.listFiles(path);
        for (FTPFile file : fileList) {
            String currentFileName = file.getName();
            String localPath = "E:\\FTPFiles\\"+currentFileName;
            try (FileOutputStream fos = new FileOutputStream(localPath)) {
                this.ftp.retrieveFile(currentFileName, fos);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    } catch (IOException ioe) {
        Logger.getLogger(FTPDownloader.class.getName()).log(Level.SEVERE, null, ioe);
    }
}
}

这是我问题的答案。我修改了代码,做了一些小改动。给定的代码将获取给定ftp路径中的所有文件,并将其存储在具有文件夹结构的本地目录中

输出结构如下例所示:

E:\FtpFiles\2013\11月\11222013\**

*.*是以FTP中的文件夹结构存储的文件

操作系统:Windows 2008服务器(64位)

但是,这将在所有windows FTP服务器中工作,并将文件移动到本地系统或给定路径

谢谢

package ConnectFTP;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;


class UFTP {

public static void main(String[] args) {

    UFTP unftp = new UFTP();
    unftp.Unfile();
}

private void Unfile() {
    try {
        //Current Date, Month, Year Starts
        Date mmddyyyy = new Date();
        String today = new SimpleDateFormat("MMddyyyy").format(mmddyyyy);
        String cyear = today.substring(4, 8);
        String month[] = {"January", "February", "March", "April", "May", "June", "July", "August",
            "September", "October", "November", "December"};
        String cmonthS = today.substring(0, 2);
        int cmonthI = Integer.parseInt(cmonthS);
        cmonthS = month[cmonthI - 1];
        System.out.println("Month : " + cmonthS);
        System.out.println("Year  : " + cyear);
        //Current Date, Month, Year Ends

        FTPClient ftp = new FTPClient();
        ftp.connect("192.168.3.5");
        if (!ftp.login("dev", "password")) {
            ftp.logout();
        }
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
        }
        ftp.enterLocalPassiveMode();
        String ftpBasePath = "devpath";  //ftp Base Directory
        String localPath = "e:\\FtpFiles\\";

        //create DIR Tree
        //Create Year Dir
        File newDir = new File(localPath + cyear);
        if (!newDir.exists()) {
            System.out.println("creating directory: " + today);
            boolean result = newDir.mkdir();
            if (result) {
                System.out.println("Year DIR Created");
            }
        }
        localPath = localPath + cyear + "\\";

        //Create Month Dir
        newDir = new File(localPath + cmonthS);
        if (!newDir.exists()) {
            System.out.println("creating directory: " + today);
            boolean result = newDir.mkdir();
            if (result) {
                System.out.println("Month DIR created");
            }
        }
        localPath = localPath + cmonthS + "\\";

        //Create Date Dir  
        newDir = new File(localPath + today);
        if (!newDir.exists()) {
            System.out.println("creating directory: " + today);
            boolean result = newDir.mkdir();
            if (result) {
                System.out.println("Today DIR created");
            }
        }

        //creates today's dir
        //destinationBasePath = "e:\\FtpFiles\\" + today + "\\";
        localPath = localPath + today + "\\";
        FTPFile[] fileList = ftp.listDirectories("/" + ftpBasePath);
        for (FTPFile directory : fileList) {
            System.out.println(directory.getName());
            String currentDir = directory.getName();
            FTPFile[] ftpFiles = ftp.listFiles("/" + ftpBasePath + "/" + currentDir + "/");
            if (ftpFiles != null && ftpFiles.length > 0) {
                for (FTPFile file : ftpFiles) {
                    if (!file.isFile()) {
                        continue;
                    }
                    System.out.println("File is " + file.getName());
                    OutputStream output;

                    //Create Dir Starts
                    newDir = new File(localPath + currentDir);
                    // if the directory does not exist, create it
                    if (!newDir.exists()) {
                        System.out.println("creating directory: " + currentDir);
                        boolean result = newDir.mkdir();
                        if (result) {
                            System.out.println("DIR created");
                        }
                    }
                    //Create Dir Ends

                    output = new FileOutputStream(localPath + currentDir + "\\" + file.getName());
                    ftp.retrieveFile("/" + ftpBasePath + "/" + currentDir + "/" + file.getName(), output);
                    output.flush();
                    output.close();
                }
            }
        }
        ftp.logout();
        ftp.disconnect();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
}

从FTPDownloader中删除
抛出异常
,并在方法内部使用
try catch
。这会告诉你哪里不对,谢谢你。但我没有得到任何例外或错误。一切正常,文件已下载,但文件大小为0 KB。这就是我找不到的问题。有人能帮我解决这个问题吗?您使用的是什么环境(windows/mac/linux 32/64位)?