如何从java中的ftp服务器读取多个文件?

时间:2017-05-22 05:12:35

标签: java ftp

我有ftp服务器如下

MainFolder 
 -Location1
   -download.txt
   -otherFiles...
 -Location2
   -download.txt
   -otherFiles...
 -Location3
   -download.txt
   -otherFiles...
 -LocationN
   -download.txt
   -otherFiles...

我想从每个Location文件夹中读取download.txt文件并插入db

我编写了以下代码,但它只读取download.txtLocation1个文件夹的文件,但它不读取其余Location文件夹中的其他download.txt个文件。

我的代码出了什么问题..?

以下是我的代码

public class ReadFtpFile {

    static Logger logger = Logger.getLogger(ReadFtpFile.class);
    public static String MAIN_FOLDER_PATH = "MAIN_FOLDER";
    public static String fileNameToBeRead = "download";


     public static void main(String[] args) {

      String ftpUrl = "10.x.x.x";  
      String user = "username";
      String pass = "password";

      FTPFile[] listOfFolders =null;
      int location = 0;
      FTPFile[] listOfFiles = null;

       // get an ftpClient object  
       FTPClient ftpClient = new FTPClient();  


        // pass directory path on server to connect  
        ftpClient.connect(ftpUrl);  

        // pass username and password, returned true if authentication is successful  
        boolean login = ftpClient.login(user, pass); 

        FTPFileFilter filter = new FTPFileFilter() {
               @Override
               public boolean accept(FTPFile ftpFile) {
                  return (ftpFile.isFile() && ftpFile.getName().contains(fileNameToBeRead));
               }
        };


        if (login) {  
         logger.info("Connection established...");  

         FTPFile[] locationfolders = ftpClient.listDirectories(MAIN_FOLDER_PATH);  

         for (FTPFile folder : locationfolders) {  

          if(folder.isDirectory()){
              logger.info("Folder Found : "  + folder.getName());
              String subFolder = MAIN_FOLDER_PATH + "/" +folder.getName();
              FTPFile[] subfiles = ftpClient.listFiles(subFolder,filter); 
              for(FTPFile myfile : subfiles){
                  logger.info("File Name: "  + myfile.getName());
                  BufferedReader in = new BufferedReader(new InputStreamReader(ftpClient.retrieveFileStream(subFolder+ "/" +myfile.getName())));
                  logger.info("Reading file start.");

                  String inputLine;
                  while ((inputLine = in.readLine()) != null){
                   // logger.info(inputLine);
                  }
                  in.close();
                  logger.info("File Reading Completed");
              }
          }

         } 

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

输出:

Connection established...
22-05-2017 10:54:49 INFO  ReadFtpFile:74 - Folder Found : Location1
22-05-2017 10:54:49 INFO  ReadFtpFile:78 - File Name: download.txt
22-05-2017 10:54:49 INFO  ReadFtpFile:80 - Reading file start.
22-05-2017 10:54:49 INFO  ReadFtpFile:87 - File Reading Completed
22-05-2017 10:54:49 INFO  ReadFtpFile:74 - Folder Found : Location2
22-05-2017 10:54:49 INFO  ReadFtpFile:74 - Folder Found : Location3
22-05-2017 10:54:49 INFO  ReadFtpFile:74 - Folder Found : LocationN

0 个答案:

没有答案
相关问题