Guest User

1sds

a guest
Apr 26th, 2016
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | None | 0 0
  1. Runner.java
  2. package FTP;
  3.  
  4. import org.apache.commons.net.ftp.FTP;
  5. import org.apache.commons.net.ftp.FTPClient;
  6.  
  7. import java.io.IOException;
  8.  
  9. public class Runner {
  10.     public static void main(String[] args) throws IOException {
  11.         FTPWorker worker = new FTPWorker();
  12.         FTPClient ftpClient = worker.getClient();
  13.         worker.getConnetcionAndLogin(ftpClient);
  14.         System.out.println("--------------------------------------------");
  15.         System.out.println("FTP SERVER INFO:");
  16.         System.out.println("--------------------------------------------");
  17.         worker.showServerReply(ftpClient);
  18.         System.out.println("--------------------------------------------");
  19.         System.out.println("List of files:");
  20.         System.out.println("--------------------------------------------");
  21.         worker.printFileList(ftpClient);
  22.         ftpClient.makeDirectory("Welcome");
  23.         System.out.println("--------------------------------------------");
  24.         System.out.println("List of files after meke directory \"Welcome\":");
  25.         System.out.println("--------------------------------------------");
  26.         worker.printFileList(ftpClient);
  27.         System.out.println("--------------------------------------------");
  28.         ftpClient.removeDirectory("Welcome");
  29.         System.out.println("Delete directory \'Welcome\"");
  30.         System.out.println("--------------------------------------------");
  31.         System.out.println("Start uploading file on FTP Server:");
  32.         System.out.println("--------------------------------------------");
  33.         worker.uploadFile("fileFromClient.txt", FTP.TELNET_TEXT_FORMAT, ftpClient);
  34.         System.out.println("--------------------------------------------");
  35.         System.out.println("List of files after uploading:");
  36.         System.out.println("--------------------------------------------");
  37.         worker.printFileList(ftpClient);
  38.         System.out.println("--------------------------------------------");
  39.         System.out.println("Rename file on FTP Server \"fileFromMac\" to \"fileFromServer\":");
  40.         worker.renameFileOnFtpServer(ftpClient, "fileFromMac.txt", "fileFromServer.txt");
  41.         System.out.println("fileFromMac.txt was successfully renamed to: fileFromServer.txt");
  42.         System.out.println("List of files:");
  43.         System.out.println("--------------------------------------------");
  44.         worker.printFileList(ftpClient);
  45.         System.out.println("--------------------------------------------");
  46.         System.out.printf("Download file from FTP Server:");
  47.         worker.downloadFileFromServer(ftpClient, "fileFromServer.txt", "fileFromServer.txt");
  48.         System.out.println("--------------------------------------------");
  49.         System.out.println("Delete own files:");
  50.         ftpClient.deleteFile("fileFromMac.txt");
  51.         ftpClient.deleteFile("fileFromServer.txt");
  52.         System.out.println("--------------------------------------------");
  53.         System.out.println("List of files:");
  54.         System.out.println("--------------------------------------------");
  55.         worker.printFileList(ftpClient);
  56.         ftpClient.disconnect();
  57.     }
  58. }
  59.  
  60.  
  61. FTPWorker.java
  62.  
  63. package FTP;
  64.  
  65. import org.apache.commons.net.ftp.FTPClient;
  66. import org.apache.commons.net.ftp.FTPFile;
  67. import org.apache.commons.net.ftp.FTPReply;
  68.  
  69. import java.io.*;
  70. import java.text.DateFormat;
  71. import java.text.SimpleDateFormat;
  72.  
  73. public class FTPWorker {
  74.     private String server = "node0.net2ftp.ru";
  75.     private static final int port = 21;
  76.     private String user = "LynX7129@gmail.com";
  77.     private String pass = "2321exa1";
  78.  
  79.     public FTPClient getClient() {
  80.         return new FTPClient();
  81.     }
  82.  
  83.     public FTPWorker(String server, String user, String pass) {
  84.         this.server = server;
  85.         this.user = user;
  86.         this.pass = pass;
  87.     }
  88.  
  89.     public FTPWorker() {
  90.     }
  91.  
  92.     public void showServerReply(FTPClient ftpClient) {
  93.         String[] replies = ftpClient.getReplyStrings();
  94.         if (replies != null && replies.length > 0) {
  95.             for (String aReply : replies)
  96.                 System.out.println("SERVER: " + aReply);
  97.         }
  98.     }
  99.  
  100.     public FTPClient getConnetcionAndLogin(FTPClient ftpClient) {
  101.         try {
  102.             ftpClient.connect(server, port);
  103.             int replyCode = ftpClient.getReplyCode();
  104.             if (!FTPReply.isPositiveCompletion(replyCode)) {
  105.                 System.out.println("Operation failed. Server reply code: " + replyCode);
  106.             }
  107.             boolean success = ftpClient.login(user, pass);
  108.             if (!success)
  109.                 System.out.println("Could not login to the server");
  110.             else
  111.                 System.out.println("LOGGED IN SERVER");
  112.  
  113.         } catch (IOException e) {
  114.             e.printStackTrace();
  115.         }
  116.         return ftpClient;  }
Add Comment
Please, Sign In to add comment