Loading
Java FTP example

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  Java FTP example


Ans)

FTP is one of the very important tools which could be used as file based integration tool.
This example illustrates how to use Apache “FTPSClient” class for transferring the files securely. 

Please down load Apache common's FTP jars.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.SocketTimeoutException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPSClient;

import com.enterprisedt.net.ftp.FileTransferClient;

public class TransferFiles {

 public void uploadFTPSFile(String localFilePath, String remoteFilePath,
   String fileName, String host, String port, String userName, String password) throws Exception{
  String localFileName = localFilePath + fileName;
  String remoteFileName = remoteFilePath + fileName;
  InputStream fileInputStream = null;
  FTPSClient ftpSClient = null;  
 
  try {   
   log.warn("Started uploading file: " + localFileName);
   ftpSClient = new FTPSClient("SSL");
   String[] protocolVersions = {"SSLv3"};
   ftpSClient.setConnectTimeout(120000);
   ftpSClient.setEnabledProtocols(protocolVersions);
   ftpSClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));   
   ftpSClient.connect(host, 990);
   log.warn("Connected to the server: " + host + ":" + port);   
   ftpSClient.login(userName, password);
   log.warn("Login successful.");   
   ftpSClient.setSoTimeout(120000);
   ftpSClient.enterLocalPassiveMode();
   ftpSClient.setFileType(FTPClient.BINARY_FILE_TYPE);
   ftpSClient.changeWorkingDirectory(remoteFilePath);
   if(ftpSClient.isConnected()) {
    ftpSClient.execPBSZ(0);
    ftpSClient.execPROT("P");
   }   
     
   fileInputStream = new FileInputStream(localFileName);
   ftpSClient.storeFile(fileName, fileInputStream);   
   log.warn("End uploading the file from " + localFileName); 
   
  }catch (Exception ex) {
   log.warn("Exception occured while uploading the file to "
     + remoteFileName);
   log.warn("Exception is " + ex);
   ex.printStackTrace();
   
   throw new Exception("Exception while uploading the file ", ex);
  }finally{
   if(fileInputStream != null) {
    fileInputStream.close();
   }      
   if(ftpSClient.isConnected()) {
    ftpSClient.logout();
    ftpSClient.disconnect();
   } 
  }
 }
 
 public boolean downloadFTPSFile(String localFilePath, String remoteFilePath,
   String fileName, String host, String port, String userName, String password) throws Exception {
  String localFileName = localFilePath + fileName;
  String remoteFileName = remoteFilePath + fileName;
  OutputStream fileOutputStream = null;
  FTPSClient ftpSClient = null; 
  boolean fileExists = true;
  
  try {
   log.warn("Started downloading the file from " + remoteFileName);
   log.warn("Started uploading file: " + localFileName);
   ftpSClient = new FTPSClient("SSL");
   String[] protocolVersions = {"SSLv3"};
   ftpSClient.setEnabledProtocols(protocolVersions);
   ftpSClient.setConnectTimeout(120000);
   ftpSClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
   ftpSClient.connect(host, 990);
   log.warn("Connected to the server: " + host + ":" + port);
   ftpSClient.login(userName, password);
   log.warn("Login successful.");
   ftpSClient.setSoTimeout(120000);
   ftpSClient.enterLocalPassiveMode();
   ftpSClient.setFileType(FTPClient.BINARY_FILE_TYPE);
   ftpSClient.changeWorkingDirectory(remoteFilePath);
   if(ftpSClient.isConnected()) {
    ftpSClient.execPBSZ(0);
    ftpSClient.execPROT("P");
   }
   fileOutputStream = new FileOutputStream(localFileName);
   log.warn("Remote file " + remoteFileName);
   fileExists = ftpSClient.retrieveFile(fileName, fileOutputStream);
   try{
    if(!fileExists) {
     
     File file = new File(localFileName);
     log.warn("Size of the file " + file.length());    
     if(file.exists() && file.length() == 0) {
      log.warn("Deleting the file: " + file.getName());
      file.delete();
      log.warn("Deleted the file: ");
     }
    }
   }catch(Exception exc) {
    log.warn("Error while deleting file");
   }   
   
   return fileExists;
  } catch (Exception ex) {
   log.debug("Exception occured while downloading the file to "
     + localFileName);
   log.error("Exception is " + ex);
   throw new Exception("Exception while uploading the file ", ex);
  }finally{
   if(fileOutputStream != null) {
    fileOutputStream.close();
   }      
   if(ftpSClient.isConnected()) {
    ftpSClient.logout();
    ftpSClient.disconnect();
   }  
  }
 }
 }



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

Java FTP example


Java FTP example

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free