헤르메스 LIFE

[Source] Apache commons FTPClient Java example - Download files from server 본문

Core Java

[Source] Apache commons FTPClient Java example - Download files from server

헤르메스의날개 2022. 3. 15. 18:45
728x90

출처 :  http://www.mysamplecode.com/2012/03/apache-commons-ftpclient-java-example_16.html


	package com.as400samplecode;
	 
	import java.io.FileInputStream;
	import java.io.FileOutputStream;
	import java.io.OutputStream;
	import java.util.Properties;
	 
	import org.apache.commons.net.ftp.FTPClient;
	import org.apache.commons.net.ftp.FTPFile;
	import org.apache.commons.net.ftp.FTPReply;
	 
	public class GetMyFiles {
	 
	    static Properties props;
	 
	    public static void main(String[] args) {
	 
	        GetMyFiles getMyFiles = new GetMyFiles();
	        if (args.length < 1)
	        {
	            System.err.println("Usage: java " + getMyFiles.getClass().getName()+
	            " Properties_file");
	            System.exit(1);
	        }
	 
	        String propertiesFile = args[0].trim();
	        getMyFiles.startFTP(propertiesFile);
	 
	    }
	 
	    public boolean startFTP(String propertiesFile){
	 
	        props = new Properties();
	 
	        try {
	 
	            props.load(new FileInputStream("properties/" + propertiesFile));
	 
	            String serverAddress = props.getProperty("serverAddress").trim();
	            String userId = props.getProperty("userId").trim();
	            String password = props.getProperty("password").trim();
	            String remoteDirectory = props.getProperty("remoteDirectory").trim();
	            String localDirectory = props.getProperty("localDirectory").trim();
	            
	            //new ftp client
	            FTPClient ftp = new FTPClient();
	            //try to connect
	            ftp.connect(serverAddress);
	            //login to server
	            if(!ftp.login(userId, password))
	            {
	                ftp.logout();
	                return false;
	            }
	            int reply = ftp.getReplyCode();
	            //FTPReply stores a set of constants for FTP reply codes.
	            if (!FTPReply.isPositiveCompletion(reply))
	            {
	                ftp.disconnect();
	                return false;
	            }
	 
	            //enter passive mode
	            ftp.enterLocalPassiveMode();
	            //get system name
	            System.out.println("Remote system is " + ftp.getSystemType());
	            //change current directory
	            ftp.changeWorkingDirectory(remoteDirectory);
	            System.out.println("Current directory is " + ftp.printWorkingDirectory());
	              
	            //get list of filenames
	            FTPFile[] ftpFiles = ftp.listFiles(); 
	            
	            if (ftpFiles != null && ftpFiles.length > 0) {
	                //loop thru files
	                for (FTPFile file : ftpFiles) {
	                    if (!file.isFile()) {
	                        continue;
	                    }
	                    System.out.println("File is " + file.getName());
	                    
	                    
	                    //get output stream
	                    OutputStream output;
	                    output = new FileOutputStream(localDirectory + "/" + file.getName());
	                    //get the file from the remote system
	                    ftp.retrieveFile(file.getName(), output);
	                    //close output stream
	                    output.close();
	                    
	                    //delete the file
	                    ftp.deleteFile(file.getName());
	                    
	                }
	            }
	            
	            ftp.logout();
	            ftp.disconnect();
	        }
	        catch (Exception ex)
	        {
	            ex.printStackTrace();
	            return false;
	        }
	        return true;
	    }
	 
	}

 

#Properties File FTP to server
 
serverAddress=ftp.someDomain.com
userId=yourUserId  
password=yourPassword
remoteDirectory=/remote/MyFolder
localDirectory=/local/MyFolder

package lib;

 import org.apache.commons.net.ftp.FTP;
 import org.apache.commons.net.ftp.FTPFile;
 import org.apache.commons.net.ftp.FTPFileFilter;
 import org.apache.commons.net.ftp.FTPSClient;

 public class FTPClientTest {
     private static FTPSClient client = new FTPSClient();
    
     public static void main(String[] args) {
         try {
             client.connect("dt.kcmkt.com", 21);
             System.out.println("wDConnect() : " + client.getReplyString());
             // passivemode
             client.enterRemotePassiveMode();
             System.out.println("wDConnect() : " + client.getReplyString());
             client.enterLocalPassiveMode();
             System.out.println("wDConnect() : " + client.getReplyString());
            
         } catch (Exception e) {
             e.printStackTrace();
         }
        
         if (client.isConnected()) {
             System.out.println("wDConnect() : " + client.isConnected());
         } else {
             System.out.println("FTP connect fail!!");
         }

         try {
             if (client.login("WKRY000001@dtwkry.kcmkt.com", "v6c5X$z3")) {
                 System.out.println("FTP login ok ................... !!");
             } else {
                 System.out.println("FTP login connect fail!!");
             }
         } catch (Exception e) {
             e.printStackTrace();
         }

         try {
             client.changeWorkingDirectory("/Prod/Out/");
             System.out.println("wDChangeWorkDir() : " + client.getReplyString());

             FTPFile[] FTPFILELIST = client.listFiles();
             for (FTPFile ftpFile : FTPFILELIST) {
                 System.out.println("wDChangeWorkDir() : " + ftpFile.toString());
             }
         } catch (Exception e) {
             e.printStackTrace();
         }

         try {
             client.logout();
             System.out.println("FTP client logout !!");
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             if (client.isConnected()) {
                 try {
                     client.disconnect();
                     System.out.println("FTP client disconnect !!");
                 } catch (Exception ie) {
                     ie.printStackTrace();
                 }
             }
         }
     }
 }
728x90

'Core Java' 카테고리의 다른 글

CXF 2.3.11 설정  (0) 2022.08.11
[Open Source] MySql Password Encoder  (0) 2022.03.21
[Core Java] File List 떨구는 샘플  (0) 2021.03.23
[Core Java] 외부명령 실행하기  (0) 2021.03.23
[Core Java] QRCode 출력  (0) 2021.03.23