Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package com.baltinfo.radius.ftp;
  2.  
  3. import org.junit.Test;
  4.  
  5. import java.io.IOException;
  6. import java.util.List;
  7.  
  8. public class FtpClientTest {
  9.  
  10.     private FtpClient ftpClient;
  11.     @Test
  12.     public void test() throws IOException {
  13.         ftpClient = new FtpClient("77.221.130.27", 21, "z102795_asv","i846f2HsPF");
  14.         //ftpClient = new FtpClient("72.163.7.54", 21, "anonymous","i846f2HsPF");
  15.         ftpClient.open();
  16.         List<String> files = ftpClient.listFiles("");
  17.         for(String file : files) {
  18.             ftpClient.downloadFile(file, "TEST" + file);
  19.         }
  20.         ftpClient.close();
  21.         return;
  22.     }
  23. }
  24.  
  25.  
  26.  
  27.  
  28. package com.baltinfo.radius.ftp;
  29.  
  30. import org.apache.commons.net.PrintCommandListener;
  31. import org.apache.commons.net.ftp.FTP;
  32. import org.apache.commons.net.ftp.FTPClient;
  33. import org.apache.commons.net.ftp.FTPFile;
  34. import org.apache.commons.net.ftp.FTPReply;
  35.  
  36. import java.io.FileOutputStream;
  37. import java.io.IOException;
  38. import java.io.PrintWriter;
  39. import java.util.Arrays;
  40. import java.util.List;
  41. import java.util.stream.Collectors;
  42.  
  43. public class FtpClient {
  44.  
  45.     private String server;
  46.     private int port;
  47.     private String user;
  48.     private String password;
  49.     private FTPClient ftpClient;
  50.  
  51.     FtpClient(String server, int port, String user, String password) {
  52.         this.server = server;
  53.         this.port = port;
  54.         this.user = user;
  55.         this.password = password;
  56.         this.ftpClient = new FTPClient();
  57.     }
  58.  
  59.     public void open() throws IOException {
  60.         //ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
  61.  
  62.         ftpClient.connect(server, port);
  63.         int reply = ftpClient.getReplyCode();
  64.       //  FTPReply rep = new FTPReply();
  65.         if(!FTPReply.isPositiveCompletion(reply)) {
  66.             ftpClient.disconnect();
  67.             throw new IOException("The first Exception!");
  68.         }
  69.         ftpClient.login(user, password);
  70.         ftpClient.enterLocalPassiveMode();
  71.         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  72.         ftpClient.changeWorkingDirectory("/Ot_ASV/2020/2020-01/2020-01-30/605/ФоÑ\u0082о_Ð\u009BоÑ\u0082ов");
  73.         FTPFile[] files =ftpClient.listDirectories();
  74.         return;
  75.     }
  76.  
  77.     public List<String> listFiles(String path) throws IOException {
  78.         FTPFile[] files = ftpClient.listFiles(path);
  79.         return Arrays.stream(files)
  80.                 .map(FTPFile::getName)
  81.                 .collect(Collectors.toList());
  82.     }
  83.     public void close() throws IOException {
  84.         ftpClient.disconnect();
  85.     }
  86.     public void downloadFile(String source, String destination) throws IOException {
  87.         if ((!source.equals(".")) && (!source.equals(".."))) {
  88.             FileOutputStream out = new FileOutputStream(destination);
  89.             ftpClient.retrieveFile(source, out);
  90.             out.close();
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement