Thursday, April 17, 2014

FTPClient

Interesting simple FTP Client for upload and download, using Apache FTPClient hehe :D

1:  import java.io.File;
2:  import java.io.FileInputStream
3:  import java.io.FileOutputStream;  
4:  import java.io.IOException;  
5:  import java.io.InputStream;  
6:  import java.io.PrintWriter;  
7:  import java.util.logging.Level;  
8:  import java.util.logging.Logger;  
9:     
10:  import org.apache.commons.net.PrintCommandListener;  
11:  import org.apache.commons.net.ftp.FTP;  
12:  import org.apache.commons.net.ftp.FTPClient; 
13:  import org.apache.commons.net.ftp.FTPReply;  
14:    
15:  /**  
16:   * This service used for upload file, upload directory, or download file to or from FTP server  
17:   * @author gopal  
18:   */  
19:  public class FTPServices {  
20:     
21:    FTPClient ftp = null;  
22:          
23:    public FTPServices(String host, String user, String pass) throws Exception {  
24:      ftp = new FTPClient();  
25:      ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));  
26:      int reply;  
27:        
28:      ftp.connect(host);  
29:      reply = ftp.getReplyCode();  
30:      if (!FTPReply.isPositiveCompletion(reply)) {  
31:        ftp.disconnect();  
32:        throw new Exception("Exception in connecting to FTP Server");  
33:      }  
34:      ftp.login(user, pass);  
35:      ftp.setFileType(FTP.BINARY_FILE_TYPE);  
36:      ftp.enterLocalPassiveMode();  
37:    }  
38:      
39:    public void uploadFile(String localFileFullName, String fileName, String hostDir) throws Exception {  
40:      try(InputStream input = new FileInputStream(new File(localFileFullName))){  
41:        this.ftp.makeDirectory(hostDir);  
42:        this.ftp.storeFile(hostDir + fileName, input);  
43:      }  
44:    }  
45:      
46:    public void uploadDirectory(String remoteDirPath, String localParentDir)  
47:        throws IOException {  
48:    
49:      File localDir = new File(localParentDir);  
50:      File[] subFiles = localDir.listFiles();  
51:      if (subFiles != null && subFiles.length > 0) {  
52:        for (File item : subFiles) {  
53:          String remoteFilePath = remoteDirPath + "/" + item.getName();  
54:    
55:          if (item.isFile()) {  
56:            // upload the file  
57:            String localFilePath = item.getAbsolutePath();  
58:            System.out.println("ABOUT UPLOAD FILE FROM: " + localFilePath);  
59:            try {  
60:              uploadFile(localFilePath, item.getName(), remoteDirPath);  
61:              System.out.println("UPLOADED FILE SUCCESS TO: "+ remoteFilePath);  
62:            } catch (Exception ex) {  
63:              Logger.getLogger(FTPServices.class.getName()).log(Level.SEVERE, null, ex);  
64:            }  
65:              
66:          } else {  
67:            // create directory on the server  
68:            this.ftp.makeDirectory(remoteFilePath);            
69:    
70:            // upload the sub directory  
71:            localParentDir = item.getAbsolutePath();  
72:            uploadDirectory(remoteDirPath, localParentDir);  
73:          }  
74:        }  
75:      }  
76:    }  
77:      
78:    public void downloadFile(String remoteFilePathFullName, String localFilePathFullName) {  
79:      try (FileOutputStream output = new FileOutputStream(localFilePathFullName)) {  
80:        this.ftp.retrieveFile(remoteFilePathFullName, output);  
81:      } catch (IOException e) {  
82:        System.out.println("ERROR DOWNLOAD");  
83:      }  
84:    }  
85:      
86:    public void disconnect(){  
87:      if (this.ftp.isConnected()) {  
88:        try {  
89:          this.ftp.logout();  
90:          this.ftp.disconnect();  
91:        } catch (IOException f) {  
92:        }  
93:      }  
94:    }         
95:  }  

And this is simple example how to use FTPService above, enjoy :)

1:        private final String FTP_SERVER_ADDRESS = "192.168.10.88";  
2:        private final String FTP_USERNAME = "admin";  
3:        private final String FTP_PASSWORD = "admin";  
4:          
5:        System.out.println("50_UPLOAD_FILE_TO_FTP");    
6:        try {  
7:          //open connection FTP  
8:          ftp = new FTPServices(FTP_SERVER_ADDRESS,FTP_USERNAME, FTP_PASSWORD);  
9:          System.out.println("50_FTP_CONNECT");   
10:        } catch (Exception ex) {  
11:          Logger.getLogger(FTPService.class.getName()).log(Level.SEVERE, null, ex);  
12:        }  
13:        try {  
14:          //upload file to FTP  
15:          String localdir = "D:/image.jpg";  
16:          String filename = "image.jpg";  
17:          String hostdir = "UploadFTP/";   
18:            
19:          ftp.uploadFile(localdir, filename, hostdir);  
20:          System.out.println("50_FILE_UPLOADED_TO_FTP");   
21:            
22:        } catch (Exception ex) {  
23:          Logger.getLogger(FTPService.class.getName()).log(Level.SEVERE, null, ex);  
24:        }  
25:        //disconnect from FTP  
26:        ftp.disconnect();  
27:        System.out.println("50_FTP_DISCONNECT");  

No comments: