Advertisement
Guest User

Untitled

a guest
Dec 24th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.22 KB | None | 0 0
  1. static void listDirectory(FTPClient ftpClient, String parentDir,
  2.     String currentDir, int level) throws IOException {
  3.     String dirToList = parentDir;
  4.     if (!currentDir.equals("")) {
  5.         dirToList += "/" + currentDir;
  6.     }
  7.     FTPFile[] subFiles = ftpClient.listFiles(dirToList);
  8.     if (subFiles != null && subFiles.length > 0) {
  9.         for (FTPFile aFile : subFiles) {
  10.             String currentFileName = aFile.getName();
  11.             if (currentFileName.equals(".")
  12.                     || currentFileName.equals("..")) {
  13.                 // skip parent directory and directory itself
  14.                 continuimport java.io.IOException;
  15. import org.apache.commons.net.ftp.FTPClient;
  16. import org.apache.commons.net.ftp.FTPFile;
  17. import org.apache.commons.net.ftp.FTPReply;
  18. public class FTPListRecursiveDemo {
  19.     static void listDirectory(FTPClient ftpClient, String parentDir, String currentDir, int level) throws IOException {
  20.         String dirToList = parentDir;
  21.         if (!currentDir.equals("")) {
  22.             dirToList += "/" + currentDir;
  23.         }
  24.         FTPFile[] subFiles = ftpClient.listFiles(dirToList);
  25.         if (subFiles != null && subFiles.length > 0) {
  26.             for (FTPFile aFile : subFiles) {
  27.                 String currentFileName = aFile.getName();
  28.                 if (currentFileName.equals(".")
  29.                         || currentFileName.equals("..")) {
  30.                     // skip parent directory and directory itself
  31.                     continue;
  32.                 }
  33.                 for (int i = 0; i < level; i++) {
  34.                     System.out.print("\t");
  35.                 }
  36.                 if (aFile.isDirectory()) {
  37.                     System.out.println("[" + currentFileName + "]");
  38.                     listDirectory(ftpClient, dirToList, currentFileName, level + 1);
  39.                 } else {
  40.                     System.out.println(currentFileName);
  41.                 }
  42.             }
  43.         }
  44.     }
  45.     public static void main(String[] args) {
  46.         String server = "www.myserver.com";
  47.         int port = 21;
  48.         String user = "username";
  49.         String pass = "password";
  50.         FTPClient ftpClient = new FTPClient();
  51.         try {
  52.             ftpClient.connect(server, port);
  53.             int replyCode = ftpClient.getReplyCode();
  54.             if (!FTPReply.isPositiveCompletion(replyCode)) {
  55.                 System.out.println("Connect failed");
  56.                 return;
  57.             }
  58.             boolean success = ftpClient.login(user, pass);
  59.             if (!success) {
  60.                 System.out.println("Could not login to the server");
  61.                 return;
  62.             }
  63.             String dirToList = "/public_html/images/articles";
  64.             listDirectory(ftpClient, dirToList, "", 0);
  65.         } catch (IOException ex) {
  66.             System.out.println("Oops! Something wrong happened");
  67.             ex.printStackTrace();
  68.         } finally {
  69.             // logs out and disconnects from server
  70.             try {
  71.                 if (ftpClient.isConnected()) {
  72.                     ftpClient.logout();
  73.                     ftpClient.disconnect();
  74.                 }
  75.             } catch (IOException ex) {
  76.                 ex.printStackTrace();
  77.             }
  78.         }
  79.     }
  80. }e;
  81.             }
  82.             for (int i = 0; i < level; i++) {
  83.                 System.out.print("\t");
  84.             }
  85.             if (aFile.isDirectory()) {
  86.                 System.out.println("[" + currentFileName + "]");
  87.                 listDirectory(ftpClient, dirToList, currentFileName, level + 1);
  88.             } else {
  89.                 System.out.println(currentFileName);
  90.             }
  91.         }
  92.     }
  93. }
  94.  
  95.  
  96. FTPListRecursiveDemo.java:1: error: class, interface, or enum expected
  97. static void listDirectory(FTPClient ftpClient, String parentDir,
  98.        ^
  99. FTPListRecursiveDemo.java:4: error: class, interface, or enum expected
  100.     if (!currentDir.equals("")) {
  101.     ^
  102. FTPListRecursiveDemo.java:6: error: class, interface, or enum expected
  103.     }
  104.     ^
  105. FTPListRecursiveDemo.java:8: error: class, interface, or enum expected
  106.     if (subFiles != null && subFiles.length > 0) {
  107.     ^
  108. FTPListRecursiveDemo.java:11: error: class, interface, or enum expected
  109.             if (currentFileName.equals(".")
  110.             ^
  111. FTPListRecursiveDemo.java:80: error: class, interface, or enum expected
  112. }e;
  113.  ^
  114. FTPListRecursiveDemo.java:81: error: class, interface, or enum expected
  115.             }
  116.             ^
  117. FTPListRecursiveDemo.java:82: error: class, interface, or enum expected
  118.             for (int i = 0; i < level; i++) {
  119.                             ^
  120. FTPListRecursiveDemo.java:82: error: class, interface, or enum expected
  121.             for (int i = 0; i < level; i++) {
  122.                                        ^
  123. FTPListRecursiveDemo.java:84: error: class, interface, or enum expected
  124.             }
  125.             ^
  126. FTPListRecursiveDemo.java:87: error: class, interface, or enum expected
  127.                 listDirectory(ftpClient, dirToList, currentFileName, level + 1);
  128.                 ^
  129. FTPListRecursiveDemo.java:88: error: class, interface, or enum expected
  130.             } else {
  131.             ^
  132. FTPListRecursiveDemo.java:90: error: class, interface, or enum expected
  133.             }
  134.             ^
  135. 13 errors
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement