Skip to Content

This code will help to copy(download) the file from source ftp location and perform some operation and place the file in the local target folder. I am not sure whether this idea can be used in generalised cases but it helps in a specific cases. In our project we had a scenario when we needed to carry on this action parallelly so we opted for java mapping to connect to FTP server to download files.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;

public class FTPDownloader extends AbstractTransformation{


FTPClient ftpObj = null;
String usrName = “”;
String pswd = “”;
String serverAdd = “”;
String srcLoc = “”;
String trgLoc = “”;
String fileName = “”;
int reply;

@Override
public void transform(TransformationInput arg0, TransformationOutput arg1)
   throws StreamTransformationException {

     // Dynamic configuration — values will be passed through interface determination.

  usrName = arg0.getInputParameters().getString(“USERNAME”);
  pswd = arg0.getInputParameters().getString(“PASSWD”);
  serverAdd = arg0.getInputParameters().getString(“SERVERADD”);
  srcLoc = arg0.getInputParameters().getString(“SOURCE”);
  trgLoc = arg0.getInputParameters().getString(“TARGET”);
  fileName = arg0.getInputParameters().getString(“FILENAME”);
 
  // getTrace().addInfo(“Input Parameter: ” + usrName + “::” + serverAdd + “::” + fileName);

  ftpObj = new FTPClient();
  getTrace().addInfo(“FTP Connection Initiated– server :: ” + serverAdd);
  String response = Connect();
  getTrace().addInfo(response);
 
  if(FTPReply.isPositiveCompletion(reply)){
   getTrace().addInfo(“Download started :: ” + fileName);
   response = download();
   getTrace().addInfo(response);
  } else {
   getTrace().addInfo(response);
   response = “Connection Failed”;
  }
 
 
  // For setting the output argument
  String StartString = “<?xml version=\”1.0\” encoding=\”UTF-8\”?>”;
  String DocumentNameSpace = “<ns0:MT_Trigger xmlns:ns0=\”urn:test:com:test:test:test\”>”;
  String RemainingString = “<Record><Dummy/></Record></ns0:MT_Trigger>”;
  String outData = StartString + DocumentNameSpace + RemainingString;
  
  try {
   arg1.getOutputPayload().getOutputStream().write(outData.getBytes(“UTF-8”));
  } catch (IOException e) {
   e.printStackTrace();
  }

}


// To connect to the FTP server
public String Connect(){
  String response = “”;
  try {
  
    // connecting FTP server
   ftpObj.connect(serverAdd);
   reply = ftpObj.getReplyCode();
   if (!FTPReply.isPositiveCompletion(reply)) {
    response = “FTP server refused connection”;
    ftpObj.disconnect();
   }else
    response = “FTP Connection Establised”;
  
    // User Authentication Started
   ftpObj.login(usrName,pswd);
   reply = ftpObj.getReplyCode();
   if (!FTPReply.isPositiveCompletion(reply)) {
    response = response + ” User NOT Authenticated “;
    ftpObj.disconnect();
   }else{
    response = response + ” User Authenticated “;
   } 
  } catch (SocketException ex) {
   response = “Connection Failed :: Socket Exception :: ” + ex.toString();
   ex.printStackTrace();
  } catch (IOException ex) {
   response = “Connection Failed :: Input/Output Exception :: ” + ex.toString();
   ex.printStackTrace();
  } catch (Exception e){
   response = “Connection Failed :: General Exception :: ” + e.toString();
   e.printStackTrace();
  }
  return response;
}


// To download a file from the source location.

public synchronized String download(){
  String response = “”;
  String trgFilename = fileName;
  try {
    // To change the PWD(Present Working Directory)
   ftpObj.changeWorkingDirectory(srcLoc);
  
    // To copy the content from server and writing into target location
   boolean exists = (new File(“trgLoc+fileName”)).exists();

   DateFormat formatter = new SimpleDateFormat(“ddMMyyyyhhss”);
   Date dt = new Date(System.currentTimeMillis());
   // To remove redundancy of file
   if (exists){
    response = “duplilcate entry found”;
    trgFilename = fileName + formatter.format(dt) ;
   }
  
   File file = new File(trgLoc+trgFilename);
   FileOutputStream fStream = new FileOutputStream(file);
   ftpObj.retrieveFile(fileName,fStream);
   fStream.close();
   ftpObj.disconnect();
   response = “Download Successful”;
  }catch(IOException e){
   response = “Download Failed ::  Input/Output Exception :: ” + e.toString();
  }catch(Exception e){
   response = “Download Failed :: General Exception :: ” + e.toString();
  }
  return response;
}

}
interfaceDet.jpg

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply