Advertisement
Guest User

Untitled

a guest
Oct 11th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 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.  
  6.  
  7. public class FTP {
  8.  
  9. static void listDirectory(FTPClient ftpClient, String parentDir,
  10. String currentDir, int level) throws IOException {
  11. //String dirToList = parentDir;
  12. // if (!currentDir.equals("")) {
  13. // dirToList += "/" + currentDir;
  14. // }
  15. FTPFile[] subFiles = null;
  16. // ftpClient.listFiles(parentDir);
  17. try {
  18. subFiles = ftpClient.listFiles(parentDir);
  19. }catch(Exception ex){
  20. ex.printStackTrace();
  21. }
  22.  
  23.  
  24. if (subFiles != null && subFiles.length > 0) {
  25. for (FTPFile aFile : subFiles) {
  26. String currentFileName = aFile.getName();
  27. if (currentFileName.equals(".")
  28. || currentFileName.equals("..")) {
  29. // skip parent directory and directory itself
  30. continue;
  31. }
  32. for (int i = 0; i < level; i++) {
  33. System.out.print("t");
  34. }
  35. if (aFile.isDirectory()) {
  36. System.out.println("[" + currentFileName + "]");
  37. listDirectory(ftpClient, parentDir, currentFileName, level + 1);
  38. } else {
  39. System.out.println(currentFileName);
  40. }
  41. }
  42. }
  43. }
  44. public static void main(String[] args) {
  45. String server = "10.xxx.xx.xxx"; // ftp server address
  46. int port = 21; // ftp uses default port Number 21
  47. String user = "123$ftp";// username of ftp server
  48. String pass = "123$ftp"; // password of ftp server
  49. FTPClient ftpClient = new FTPClient();
  50. try {
  51. ftpClient.connect(server, port);
  52. ftpClient.login(user, pass);
  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 = "TMS$SYSDEVICE:[TMS$VMS.OPERATOR.PRINT]";
  64. listDirectory(ftpClient, dirToList, "", 1);
  65. } catch (IOException ex) {
  66. System.out.println("Oops! Something wrong happened");
  67. ex.printStackTrace();
  68. }catch(Exception ex){
  69. System.out.println("Oops! Something wrong happened");
  70. ex.printStackTrace();
  71. }
  72. finally {
  73. // logs out and disconnects from server
  74. try {
  75. if (ftpClient.isConnected()) {
  76. ftpClient.logout();
  77. ftpClient.disconnect();
  78. }
  79. } catch (IOException ex) {
  80. ex.printStackTrace();
  81. }
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement