FTP CLIENT FTP4J in JAVA

Below is the code which demonstrates the implementation of FTP4J FTP CLIENT in JAVA.

This code can be readily used in any JAVA application which has the requirement of implementing FTP CLIENT.

This FTP4J FTP client is a very reliable and the best of the Free FTP clients available.

This code also shows the  URL object implementation in JAVA.



 

Implementation:

<%@ page import="java.net.URL" %>
<%@page import="it.sauronsoftware.*" %>
<%@page import="it.sauronsoftware.ftp4j.FTPClient" %>


 try
                    {
                    //FTP CODE USING FTP4J
                     
                     //FTP conenction code    
                     
            
  
                         String ftphost=<HOST NAME>;
           String ftpuser=<FTP USER NAME>;
           String ftppassword=<FTP PASSWORD>;
           int ftpport=<FTP PORT>;
           int ftpportread=<FTP READ PORT>; //OPTIONAL
             
        //FTP CONNECTION CODE END 
          
        //Declaring FTP client & connecting to FTP
     
        FTPClient client = new FTPClient();
        
          client.connect(ftphost, ftpport);
          System.out.println("FTP Succssfully Connected");
          
        client.login(ftpuser, ftppassword);
        System.out.println("FTP Login Successful");
        
        client.setType(FTPClient.TYPE_AUTO);
        boolean compressionSupported = client.isCompressionSupported();
        
        System.out.println("compressionSupported***"+compressionSupported);
        
        client.setCompressionEnabled(true);
     //FTP client intitialization & connection END
     
     
     //CHECKING EXISTENCE of FOLDER (DATE) using HttpURLConnection
     
           URL u = new URL("http://"+ftphost+":"+ftpportread+"/collectiondoc/"+mid+"/"+sid+"/"+currentDate+"/"); 
           HttpURLConnection huc =  (HttpURLConnection)  u.openConnection(); 
           huc.setRequestMethod("GET");
           huc.connect();
           int rc = huc.getResponseCode();
      
        //CHECKING EXISTENCE of FOLDER (DATE) using HttpURLConnection END      
          
        //PATH OF TARGET LOCATION
       String targetPath="/collectiondoc/"+mid+"/"+sid+"/"+currentDate+""; 
      
        
      //IF rc=404 means FOLDER NOT EXISTS. SO create new folder 
      if(rc==404)  
                         {
                                client.createDirectory(targetPath); 
                                      
                         }//if        
                               
                         //IF rc=404 means FOLDER NOT EXISTS. SO create new folder END  
      
         client.changeDirectory(targetPath);  
         client.upload(fullFile);
         client.rename(orgFileName,fname);
         System.out.println("UPLADING FILE THROUGH FTP DONE");
                client.disconnect(true);
        
            System.out.println("Disconnected");
 
 
Note: You can also find the manual for the same at below location
 
http://www.sauronsoftware.it/projects/ftp4j/manual.php 

No comments:

Post a Comment