Advertisement
Guest User

ftp

a guest
Aug 3rd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.55 KB | None | 0 0
  1. package th.in.oneauthen;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7.  
  8. import org.apache.commons.net.ftp.FTP;
  9. import org.apache.commons.net.ftp.FTPClient;
  10. import org.apache.commons.net.ftp.FTPFile;
  11.  
  12. public class sftp {
  13.    
  14.     public static void main(String[] args) {
  15.         String server = "161.246.35.229";
  16.         int port = 22;
  17.         String user = "medserv";
  18.         String pass = "MeddyCall2018";
  19.  
  20.         FTPClient ftpClient = new FTPClient();
  21.  
  22.         try {
  23.             // connect and login to the server
  24.             ftpClient.connect(server, port);
  25.             ftpClient.login(user, pass);
  26.  
  27.             // use local passive mode to pass firewall
  28.             ftpClient.enterLocalPassiveMode();
  29.  
  30.             System.out.println("Connected");
  31.  
  32.             String remoteDirPath = "/Test";
  33.             String saveDirPath = "E:/Test/Download/FTP";
  34.  
  35.             downloadDirectory(ftpClient, remoteDirPath, "", saveDirPath);
  36.  
  37.             // log out and disconnect from the server
  38.             ftpClient.logout();
  39.             ftpClient.disconnect();
  40.  
  41.             System.out.println("Disconnected");
  42.         } catch (IOException ex) {
  43.             ex.printStackTrace();
  44.         }
  45.     }
  46.  
  47.     public static void downloadDirectory(FTPClient ftpClient, String parentDir,
  48.             String currentDir, String saveDir) throws IOException {
  49.         String dirToList = parentDir;
  50.         if (!currentDir.equals("")) {
  51.             dirToList += "/" + currentDir;
  52.         }
  53.      
  54.         FTPFile[] subFiles = ftpClient.listFiles(dirToList);
  55.      
  56.         if (subFiles != null && subFiles.length > 0) {
  57.             for (FTPFile aFile : subFiles) {
  58.                 String currentFileName = aFile.getName();
  59.                 if (currentFileName.equals(".") || currentFileName.equals("..")) {
  60.                     // skip parent directory and the directory itself
  61.                     continue;
  62.                 }
  63.                 String filePath = parentDir + "/" + currentDir + "/"
  64.                         + currentFileName;
  65.                 if (currentDir.equals("")) {
  66.                     filePath = parentDir + "/" + currentFileName;
  67.                 }
  68.      
  69.                 String newDirPath = saveDir + parentDir + File.separator
  70.                         + currentDir + File.separator + currentFileName;
  71.                 if (currentDir.equals("")) {
  72.                     newDirPath = saveDir + parentDir + File.separator
  73.                               + currentFileName;
  74.                 }
  75.      
  76.                 if (aFile.isDirectory()) {
  77.                     // create the directory in saveDir
  78.                     File newDir = new File(newDirPath);
  79.                     boolean created = newDir.mkdirs();
  80.                     if (created) {
  81.                         System.out.println("CREATED the directory: " + newDirPath);
  82.                     } else {
  83.                         System.out.println("COULD NOT create the directory: " + newDirPath);
  84.                     }
  85.      
  86.                     // download the sub directory
  87.                     downloadDirectory(ftpClient, dirToList, currentFileName,
  88.                             saveDir);
  89.                 } else {
  90.                     // download the file
  91.                     boolean success = downloadSingleFile(ftpClient, filePath,
  92.                             newDirPath);
  93.                     if (success) {
  94.                         System.out.println("DOWNLOADED the file: " + filePath);
  95.                     } else {
  96.                         System.out.println("COULD NOT download the file: "
  97.                                 + filePath);
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.     }
  103.     public static boolean downloadSingleFile(FTPClient ftpClient,
  104.             String remoteFilePath, String savePath) throws IOException {
  105.         File downloadFile = new File(savePath);
  106.          
  107.         File parentDir = downloadFile.getParentFile();
  108.         if (!parentDir.exists()) {
  109.             parentDir.mkdir();
  110.         }
  111.              
  112.         OutputStream outputStream = new BufferedOutputStream(
  113.                 new FileOutputStream(downloadFile));
  114.         try {
  115.             ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  116.             return ftpClient.retrieveFile(remoteFilePath, outputStream);
  117.         } catch (IOException ex) {
  118.             throw ex;
  119.         } finally {
  120.             if (outputStream != null) {
  121.                 outputStream.close();
  122.             }
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement