如何使用 ftpclient java 从 ftp 下载前 20 个文件



假设一个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";
}

据我了解,您希望在第一次调用时下载前 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);
           }

您可以使用迭代变量,例如 I,每次下载特定文件时,您都会计算 i 加一,如果 i>= 20,则中止函数。

相关内容

  • 没有找到相关文章

最新更新