0

我正在使用 org.apache.commons.net.ftp.FTPClient 从 ftp 服务器下载文件。

它从我的 ftp 服务器下载所有文件,除了名称为 test.xml test.txt west.xml 等的文件,这些名称的文件正在下载,文件中没有数据。retrieveFile() 方法为这些文件返回布尔值 false。

我试图通过在 ftp 服务器上手动重命名文件来下载相同的文件。它与其他名称配合得很好。

请让我知道如何解决这个问题。

编辑-添加示例代码

public static boolean downloadFTPDir(String localDir, FTPClient ftpClient) {
    OutputStream output = null;
    try {

        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        /*
         * Use passive mode as default because most of us are behind
         * firewalls these days.
         */
        ftpClient.enterLocalPassiveMode();

        FTPFile[] fileList = ftpClient.listFiles();

        for (FTPFile ftpFile : fileList) {
            if(ftpFile.isDirectory()){
                String tempDir = localDir + File.separatorChar+ftpFile.getName();
                try {
                    File temp = new File(tempDir);
                    temp.mkdirs();
                } catch (Exception e) {
                    System.out.println("Could not create local directory "+tempDir);
                    return false;
                }
                if(ftpClient.changeWorkingDirectory(ftpFile.getName())) {
                    downloadFTPDir(tempDir, ftpClient);
                } else {
                    System.out.println("Could change directory to "+ftpFile.getName()+" on FTP server");
                    return false;
                }

            } else {
                output = new FileOutputStream(localDir + File.separatorChar + ftpFile.getName());
                if (!ftpClient.retrieveFile(ftpFile.getName(), output)) {
                    System.out.println("Unable to download file from FTP server : " + ftpFile.getName());
                    File tmp = null;
                    try {
                        output.close();
                        /*tmp = new File(localDir + File.separatorChar + ftpFile.getName());
                        tmp.delete();
                        logger.info("Deleted corrupt downloaded file : " + tmp.getAbsolutePath());*/
                    } catch (FTPConnectionClosedException e) {
                        System.out.println("Connection to FTP server  is closed ");
                        return false;
                    } catch (Exception e1) {
                        System.out.println("Unable to delete corrupt file from local directory : " + tmp.getAbsolutePath());
                        return false;
                    }
                }
                output.close();
                System.out.println("FTP file download successful : " + ftpFile.getName());
            }

        }

    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
        return false;
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
        return false;
    } catch (IOException e2) {
        e2.printStackTrace();
        return false;
    } catch (Exception e3) {
        try {
            output.close();
        } catch (Exception e4) {
            e3.printStackTrace();
        }
        return false;
    } finally{
        try {
            if(output!=null)
                output.close();
        } catch (Exception e5) {
            e5.printStackTrace();
        } 
    }
    return true;
}

public static void main(String[] args) {
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect("FTP IP", 21);
        System.out.println("connecting");
        if(FTPReply.isPositiveCompletion(ftpClient.getReply())) {
            System.out.println("connected");
            if(!ftpClient.login("FTP username", "FTP Password")) {
                System.out.println("could not login");
                ftpClient.disconnect();
                return;
            }
            System.out.println("logged in");
            String remotePath = "FTP directory Path";
            StringTokenizer st = new StringTokenizer(remotePath, "/");
            String dir = null;

            boolean status = false;
                while (st.hasMoreTokens()) {
                    dir = st.nextToken();
                    status = ftpClient.changeWorkingDirectory(dir);

                    if (!status) {
                        System.out.println("FTP client is not able to change the current directory to " + dir);
                        return ;
                    }

                }
            System.out.println("connected");
            downloadFTPDir("local path", ftpClient);
        } else {
            ftpClient.disconnect();
        }
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
4

0 回答 0