Advertisement
Guest User

Untitled

a guest
Dec 24th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. import java.io.IOException;
  2. import org.apache.commons.net.ftp.FTPClient;
  3. import org.apache.commons.net.ftp.FTPFile;
  4. import org.apache.commons.net.ftp.FTPReply;
  5. public class FTPListRecursiveDemo {
  6.     static void listDirectory(FTPClient ftpClient, String parentDir,
  7.             String currentDir, int level) throws IOException {
  8.         String dirToList = parentDir;
  9.         if (!currentDir.equals("")) {
  10.             dirToList += "/" + currentDir;
  11.         }
  12.         FTPFile[] subFiles = ftpClient.listFiles(dirToList);
  13.         if (subFiles != null && subFiles.length > 0) {
  14.             for (FTPFile aFile : subFiles) {
  15.                 String currentFileName = aFile.getName();
  16.                 if (currentFileName.equals(".")
  17.                         || currentFileName.equals("..")) {
  18.                     // skip parent directory and directory itself
  19.                     continue;
  20.                 }
  21.                 for (int i = 0; i < level; i++) {
  22.                     System.out.print("\t");
  23.                 }
  24.                 if (aFile.isDirectory()) {
  25.                     System.out.println("[" + currentFileName + "]");
  26.                     listDirectory(ftpClient, dirToList, currentFileName, level + 1);
  27.                 } else {
  28.                     System.out.println(currentFileName);
  29.                 }
  30.             }
  31.         }
  32.     }
  33. public static void main(String[] args) {
  34.     String server = "www.myserver.com";
  35.     int port = 21;
  36.     String user = "username";
  37.     String pass = "password";
  38.     FTPClient ftpClient = new FTPClient();
  39.     try {
  40.         ftpClient.connect(server, port);
  41.         int replyCode = ftpClient.getReplyCode();
  42.         if (!FTPReply.isPositiveCompletion(replyCode)) {
  43.             System.out.println("Connect failed");
  44.             return;
  45.         }
  46.         boolean success = ftpClient.login(user, pass);
  47.         if (!success) {
  48.             System.out.println("Could not login to the server");
  49.             return;
  50.         }
  51.         String dirToList = "/public_html/images/articles";
  52.         listDirectory(ftpClient, dirToList, "", 0);
  53.     } catch (IOException ex) {
  54.         System.out.println("Oops! Something wrong happened");
  55.         ex.printStackTrace();
  56.     } finally {
  57.         // logs out and disconnects from server
  58.         try {
  59.             if (ftpClient.isConnected()) {
  60.                 ftpClient.logout();
  61.                 ftpClient.disconnect();
  62.             }
  63.         } catch (IOException ex) {
  64.             ex.printStackTrace();
  65.         }
  66.     }
  67. }    
  68.  
  69.  
  70.  
  71. FTPListRecursiveDemo.java:67: error: reached end of file while parsing
  72. }
  73.  ^
  74. 1 error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement