【问题标题】:How to download first 20 files from ftp using ftpclient java如何使用 ftpclient java 从 ftp 下载前 20 个文件
【发布时间】:2019-06-08 03:36:16
【问题描述】:

假设 FTP 位置有 100 个文件。每次调用该函数时,我想从 100 个文件中下载 20 个文件。当我调用 readFTP 函数时,我该如何实现?

ReadFTPread(String host, String userName, String password,String ftpDirectory,String downloadDir)

public static String read(String host, String userName, String password,String ftpDirectory,String downloadDir) {
    Logger logger = Logger.getLogger("LCAlertLog");
    // get an ftpClient object
    FTPClient ftpClient = new FTPClient();

    FileOutputStream fos = null;

    String fileName = "";

    try {

        ftpClient.connect(host);

        //boolean login = ftpClient.login(username,password);
        boolean login = ftpClient.login(userName, password);
        if (login) {
            //Logger.info(Connection established.####..");
            ftpClient.changeWorkingDirectory(ftpDirectory);

            int  returnCode = ftpClient.getReplyCode();
            logger.info("### FTP RETURN CODE for change to: "+ ftpDirectory+" :"+returnCode);
            ////System.out.println("### FTP RETURN CODE for change to: "+ ftpDirectory+" :"+returnCode);

            FTPFile[] files = ftpClient.listFiles();

            for (FTPFile file : files) {
                if (file.getType() == FTPFile.FILE_TYPE) {

                    logger.info("File Name: "+ file.getName());

                    fileName = file.getName();

                    Date lastModified = file.getTimestamp().getTime();

                    Calendar today = Calendar.getInstance();
                    // Subtract 1 day to get yesterday
                    // today.add(Calendar.DATE, -8);
                    today.add(Calendar.DATE, -0); // TODAYS DATE , put -1 to
                                                    // get YESTERDAY

                    Date yesterday = new java.sql.Date(
                            today.getTimeInMillis());

                    int flag = 0;
                    int searchDateRange = 1;

                    if (searchDateRange == 1) {

                        fos = new FileOutputStream(downloadDir + fileName);
                        boolean download = ftpClient.retrieveFile(fileName,fos);
                        logger.info("#### IS DOWNLOAD: "+download);
                        ////System.out.println("#### IS DOWNLOAD: "+download);
                        if (download) {
                            String existingFilepath = ftpDirectory;
                            String newFilepath = "backup/";
                            //ftpClient.makeDirectory(newFilepath)
                        //  //System.out.println("### FILE Current: "+existingFilepath+fileName+"### FILE NEW :"+newFilepath+fileName);

                            /*  ftpClient.rename(existingFilepath+fileName,newFilepath+fileName);*/

                            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                            ftpClient.retrieveFile(fileName, outputStream);
                            InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
                            //now, store this stream to backup directory. First we need to change working directory to backup directory.

                            // assuming backup directory is with in current working directory
                            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//binary files
                            ftpClient.changeWorkingDirectory(newFilepath);


                             returnCode = ftpClient.getReplyCode();
                             logger.info("### FTP RETURN CODE for : "+newFilepath+" :"+returnCode);
                             ////System.out.println("### FTP RETURN CODE for : "+newFilepath+" :"+returnCode);
                                if (returnCode == 550) {
                                    logger.warn("### Change dir failed return code"+returnCode);
                                    ////System.out.println("Change dir failed");
                                }

                            //this overwrites the existing file
                                logger.info("#### FTP CLIENT STORE FILE: "+ftpClient.storeFile(fileName, is));
                            ////System.out.println("#### FTP CLIENT STORE FILE: "+ftpClient.storeFile(fileName, is));
                            ftpClient.changeWorkingDirectory("../");
                            logger.info("### FTP RETURN CODE for Previous Dir :"+returnCode);
                            ////System.out.println("### FTP RETURN CODE for Previous Dir :"+returnCode);
                            ftpClient.deleteFile(fileName);
                            returnCode = ftpClient.getReplyCode();
                            logger.info("### FTP RETURN CODE for delete : "+ftpDirectory + fileName+" :"+returnCode);
                        //  //System.out.println("### FTP RETURN CODE for delete : "+ftpDirectory + fileName+" :"+returnCode);
                            //if you don't want to overwrite it use storeUniqueFile

                            fos.close();
                        }

                    } else if (searchDateRange == 0) {

                    }

                }
            }

            // logout the user, returned true if logout successfully
            boolean logout = ftpClient.logout();
            if (logout) {
                // //Logger.info(Connection close...");
            }
        } else {
            logger.warn("Connection failed ");
        }
        // testing.closeFile();

    } catch (SocketException e) {

        logger.warn("EXCEPTION ",e);
        return "Socket Exception";
    } catch (IOException e) {
        logger.warn("EXCEPTION ",e);

        return "IOException";
    } catch (Exception e) {
        logger.warn("EXCEPTION ",e);
        return "exception";
    }

    return "Success";
}

【问题讨论】:

  • 请编辑您的文本并添加一个问题。现在只是一个声明,并没有解释代码有什么问题或有什么问题

标签: java spring spring-boot spring-mvc


【解决方案1】:

据我了解,您想在第一次通话中下载前 20 个项目。在第二次通话中,您想下载第 20 到第 40 项,就这样。

    FTPFile[] files = ftpClient.listFiles();

此方法是列出 FTP 中的所有项目。因此,您必须选择要下载的项目。

我的建议你可以调用函数,如 ;

    public static String read(String host, String userName, String password,String ftpDirectory,String downloadDir, int index) {
            ...
            for (int i = index; i<index+20; i++) {
                    FTPFile file = files[index];//you have the file

例如,您可以传递索引值 = 0,20,40,60,80;

           for(int i = 0;i<5;i++){
              read(userName,password,ftpDirectory,downloadDir,i*20);
           }

【讨论】:

  • 我想在每个方法调用中下载前 20 个文件。我不传递任何索引值。因为FTPFile[] files = ftpClient.listFiles();可以返回100多个文件。
  • 因此您可以将文件从 ftpDirectory 移动到另一个目录。示例代码在这里; stackoverflow.com/questions/11202215/… 。当您将文件移动到另一个目录时,第二个循环会再给您 20 个 FTPFiles。
【解决方案2】:

您可以使用迭代变量,例如。 G。一个我,每次你下载一个特定的文件时,你计算 i 加一,如果 i >= 20 则中止该功能。

【讨论】:

    猜你喜欢
    • 2014-11-21
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多