簡體   English   中英

如何使用 ftpclient java 從 ftp 下載前 20 個文件

[英]How to download first 20 files from ftp using ftpclient java

假設一個 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 ,則中止該函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM