有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

spring如何使用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";
}

共 (1) 个答案

  1. # 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);
               }