Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Runner.java
- package FTP;
- import org.apache.commons.net.ftp.FTP;
- import org.apache.commons.net.ftp.FTPClient;
- import java.io.IOException;
- public class Runner {
- public static void main(String[] args) throws IOException {
- FTPWorker worker = new FTPWorker();
- FTPClient ftpClient = worker.getClient();
- worker.getConnetcionAndLogin(ftpClient);
- System.out.println("--------------------------------------------");
- System.out.println("FTP SERVER INFO:");
- System.out.println("--------------------------------------------");
- worker.showServerReply(ftpClient);
- System.out.println("--------------------------------------------");
- System.out.println("List of files:");
- System.out.println("--------------------------------------------");
- worker.printFileList(ftpClient);
- ftpClient.makeDirectory("Welcome");
- System.out.println("--------------------------------------------");
- System.out.println("List of files after meke directory \"Welcome\":");
- System.out.println("--------------------------------------------");
- worker.printFileList(ftpClient);
- System.out.println("--------------------------------------------");
- ftpClient.removeDirectory("Welcome");
- System.out.println("Delete directory \'Welcome\"");
- System.out.println("--------------------------------------------");
- System.out.println("Start uploading file on FTP Server:");
- System.out.println("--------------------------------------------");
- worker.uploadFile("fileFromClient.txt", FTP.TELNET_TEXT_FORMAT, ftpClient);
- System.out.println("--------------------------------------------");
- System.out.println("List of files after uploading:");
- System.out.println("--------------------------------------------");
- worker.printFileList(ftpClient);
- System.out.println("--------------------------------------------");
- System.out.println("Rename file on FTP Server \"fileFromMac\" to \"fileFromServer\":");
- worker.renameFileOnFtpServer(ftpClient, "fileFromMac.txt", "fileFromServer.txt");
- System.out.println("fileFromMac.txt was successfully renamed to: fileFromServer.txt");
- System.out.println("List of files:");
- System.out.println("--------------------------------------------");
- worker.printFileList(ftpClient);
- System.out.println("--------------------------------------------");
- System.out.printf("Download file from FTP Server:");
- worker.downloadFileFromServer(ftpClient, "fileFromServer.txt", "fileFromServer.txt");
- System.out.println("--------------------------------------------");
- System.out.println("Delete own files:");
- ftpClient.deleteFile("fileFromMac.txt");
- ftpClient.deleteFile("fileFromServer.txt");
- System.out.println("--------------------------------------------");
- System.out.println("List of files:");
- System.out.println("--------------------------------------------");
- worker.printFileList(ftpClient);
- ftpClient.disconnect();
- }
- }
- FTPWorker.java
- package FTP;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPFile;
- import org.apache.commons.net.ftp.FTPReply;
- import java.io.*;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- public class FTPWorker {
- private String server = "node0.net2ftp.ru";
- private static final int port = 21;
- private String user = "LynX7129@gmail.com";
- private String pass = "2321exa1";
- public FTPClient getClient() {
- return new FTPClient();
- }
- public FTPWorker(String server, String user, String pass) {
- this.server = server;
- this.user = user;
- this.pass = pass;
- }
- public FTPWorker() {
- }
- public void showServerReply(FTPClient ftpClient) {
- String[] replies = ftpClient.getReplyStrings();
- if (replies != null && replies.length > 0) {
- for (String aReply : replies)
- System.out.println("SERVER: " + aReply);
- }
- }
- public FTPClient getConnetcionAndLogin(FTPClient ftpClient) {
- try {
- ftpClient.connect(server, port);
- int replyCode = ftpClient.getReplyCode();
- if (!FTPReply.isPositiveCompletion(replyCode)) {
- System.out.println("Operation failed. Server reply code: " + replyCode);
- }
- boolean success = ftpClient.login(user, pass);
- if (!success)
- System.out.println("Could not login to the server");
- else
- System.out.println("LOGGED IN SERVER");
- } catch (IOException e) {
- e.printStackTrace();
- }
- return ftpClient; }
Add Comment
Please, Sign In to add comment