skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, October 13, 2012

Secure File Transfer using java Program

Posted by Raju Gupta at 8:00 PM – 0 comments
 

This program provides transfer of all files from source directory to destination directory securely.
To compile and run this program , one should have the jars edtftpj-pro-3.1.2.jar and edtftpj-pro-license.jar handy.

How to compile :

javac -classpath $CLASSPATH:edtftpj-pro-3.1.2.jar:edtftpj-pro-license.jar secureMultipleFileTransfer.java

How to run :

1)export CLASSPATH=$CLASSPATH:edtftpj-pro-3.1.2.jar:edtftpj-pro-license.jar

2)java secureMultipleFileTransfer   

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.Protocol;
import com.enterprisedt.net.ftp.SecureFileTransferClient;
import java.io.FileReader;
import com.enterprisedt.net.ftp.FTPTransferType;


public class secureMultipleFileTransfer {

    public secureMultipleFileTransfer() {
               
    }
    
    public static void main(String[] args) throws FTPException { 
        boolean isRecursive = false;      
 SecureFileTransferClient sftp = new SecureFileTransferClient();
        String srcDirName = "/home/test/src/";          
        String destDirectory="/home/test/dest/";
   
 try { 
       File dir = new File(srcDirName);
       if(dir.exists()) {       
          String [] FileName = dir.list();
          if (null != FileName) {
           System.out.println("Number of files in source directory ** "+FileName.length);
           }         
                 System.out.println("Set host name to connect***");
          sftp.setRemoteHost("Test.Server");
   System.out.println("Set User name***");
          sftp.setUserName("abcd");
   System.out.println("Set Password***");
          sftp.setPassword("test123");
          sftp.setProtocol(Protocol.SFTP);
          Protocol p = sftp.getProtocol();      
          System.out.println("Connecting to the host*** ");          
          sftp.connect();
   System.out.println("Connected to the host*** ");
   System.out.println("Setting the mode of transfer of file*** ");      
                 sftp.setContentType(FTPTransferType.BINARY);                        
   System.out.println("Trasferring file from source to destination***");
          sftp.uploadMultiple(srcDirName,destDirectory,"*",isRecursive);
   System.out.println("Files transferred successfully***");
              }
 } catch (FileNotFoundException fe){
     fe.printStackTrace();
     System.exit(1);
 } catch (Exception ex){      
     String errorMsg = "Caught Exception ";
     System.out.println(errorMsg); 
            ex.printStackTrace();                 
  } finally{
      try{
          sftp.disconnect();  
        
      } catch (Exception ex) {      
          String errorMsg = "Caught Exception ";
          System.out.println(errorMsg); 
                 ex.printStackTrace();                 
      }
        
         }
    }
}


Labels: FTP Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger