Skip to Content

Hi All,

I wrote this basic code to solve on simple requirement in a File to File scenario where I need to put a to a SFTP location but the SFTP adapter is absent there.

I first tried by using a JAVA mapping code the code for the same is as below. I used the Jsch Library to use the connection.

Here a text file is placed to the target location to notify when a file is placed at SFTP.

Code:

i

import java.io.InputStream;

import java.io.OutputStream;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

import com.jcraft.jsch.Channel;

import com.jcraft.jsch.ChannelSftp;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.Session;

import com.sap.aii.mapping.api.AbstractTrace;

import com.sap.aii.mapping.api.StreamTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

public class SFTP implements StreamTransformation{

  private Map map = null;

  private AbstractTrace trace = null;

  public void setParameter(Map arg0) {

    map = arg0;   // Store reference to the mapping parameters

    if (map == null) {

    this.map = new HashMap();

    }

  }

  String SFTPHOST = “<IP or HOST>”;

  int SFTPPORT = <PORT>; //usually 22

  String SFTPUSER = “<USERNAME>”;

  String SFTPPASS = “<PASSWORD>;

  String destinationDir = “/”;

  Session session = null;

  Channel channel = null;

  ChannelSftp channelSftp = null;

  String fileName=””;

  /*public static void main(String args[]){  //FOR EXTERNAL STANDALONE TESTING

  try{

  FileInputStream fin = new FileInputStream (“C:/Users/ashutosh.a.upadhyay/My Documents/AA.XLS”); //INPUT FILE (PAYLOAD)

  FileOutputStream fout = new FileOutputStream (“C:/Users/ashutosh.a.upadhyay/My Documents/file.txt”); //OUTPUT FILE (PAYLOAD)

  SFTP mapping = new SFTP();

  mapping.execute(fin, fout);

  }

  catch (Exception e1){

  e1.printStackTrace();

  }

  }*/

  public void execute(InputStream inputstream, OutputStream outputstream) throws StreamTransformationException{

  try{

  Date date = new Date();

  SimpleDateFormat sdf = new SimpleDateFormat(“dd/MM/yyyy”);

  String formattedDate = sdf.format(date);

  System.out.println(formattedDate);

  fileName = “<Arbitrary name>” + formattedDate;

  //Placing file to SFTP

  JSch jsch = new JSch();

  session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);

  session.setPassword(SFTPPASS);

  java.util.Properties config = new java.util.Properties();

  config.put(“StrictHostKeyChecking”, “no”);

  System.out.println(“Establishing Connection…”);

  session.setConfig(config);

  session.connect();

  System.out.println(“Session Established”);

  channel = session.openChannel(“sftp”);

  System.out.println(“Connecting the channel..” );

  channel.connect();

  channelSftp = (ChannelSftp) channel;

  System.out.println(“Hitting the directory” );

  channelSftp.put(inputstream,fileName);

  System.out.println(“File placed..” );

  channelSftp.disconnect();

  System.out.println(“Channel Disconnected…” );

  session.disconnect();

  sdf = new SimpleDateFormat(“MM/dd/yyyy h:mm:ss a”);

  formattedDate = sdf.format(date);

  System.out.println(formattedDate);

      String file = “Output file placed to SFTP server at “+formattedDate;

                    outputstream.write(file.getBytes());

          inputstream.close();

  System.out.println(“Server Disconnected…” );

  }

  catch(Exception e){

  session.disconnect();

  channelSftp.disconnect();

  e.printStackTrace();

  throw new StreamTransformationException(“Exception occured, disconnecting.. “+ e.getMessage(), e);

  }

  }

}

Code to download the file:

import java.io.FileInputStream;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.InputStream;

import java.io.FileOutputStream;

import java.io.OutputStream;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

import com.jcraft.jsch.Channel;

import com.jcraft.jsch.ChannelSftp;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.Session;

import com.sap.aii.mapping.api.AbstractTrace;

import com.sap.aii.mapping.api.StreamTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

public class SFTPDownload implements StreamTransformation{

  private Map map = null;

  private AbstractTrace trace = null;

  public void setParameter(Map arg0) {

    map = arg0;   // Store reference to the mapping parameters

    if (map == null) {

    this.map = new HashMap();

    }

  }

  String SFTPHOST = “<IP or HOST>”;

  int SFTPPORT = <PORT>; //usually 22

  String SFTPUSER = “<USERNAME>”;

  String SFTPPASS = “<PASSWORD>;

  String destinationDir = “/”;

  Session session = null;

  Channel channel = null;

  ChannelSftp channelSftp = null;

  String fileName=””;

  /*public static void main(String args[]){  //FOR EXTERNAL STANDALONE TESTING 

  try{ 

  FileInputStream fin = new FileInputStream (“C:/Users/ashutosh.a.upadhyay/My Documents/AA.XLS”); //INPUT FILE (PAYLOAD) 

  FileOutputStream fout = new FileOutputStream (“C:/Users/ashutosh.a.upadhyay/My Documents/SFTP.XLS”); //OUTPUT FILE (PAYLOAD) 

  SFTPDownload mapping = new SFTPDownload(); 

  mapping.execute(fin, fout); 

  } 

  catch (Exception e1){ 

  e1.printStackTrace(); 

  }

  }*/

  public void execute(InputStream inputstream, OutputStream outputstream) throws StreamTransformationException{

  try{

  JSch jsch = new JSch();

  session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);

  session.setPassword(SFTPPASS);

  java.util.Properties config = new java.util.Properties();

  config.put(“StrictHostKeyChecking”, “no”);

  System.out.println(“Establishing Connection…”);

  session.setConfig(config);

  session.connect();

  System.out.println(“Session Established”);

  channel = session.openChannel(“sftp”);

  System.out.println(“Connecting the channel..” );

  channel.connect();

  channelSftp = (ChannelSftp) channel;

  System.out.println(“Hitting the directory” );

  channelSftp.cd(destinationDir);

  byte[] buffer = new byte[1024];

  BufferedInputStream bis = new BufferedInputStream(channelSftp.get(“SFTP.XLS”));

  //File newFile = new File(+fileName);

  //OutputStream os = new FileOutputStream(newFile);

  BufferedOutputStream bos = new BufferedOutputStream(outputstream);

  int readCount;

  while( (readCount = bis.read(buffer)) > 0) {

  System.out.println(“Writing: ” );

  bos.write(buffer, 0, readCount);

  }

  bis.close();

  bos.close();

  channelSftp.disconnect();

  System.out.println(“Channel Disconnected…” );

  session.disconnect();

                    outputstream.close();

         inputstream.close();

  System.out.println(“Server Disconnected…” );

  }

  catch(Exception e){

  session.disconnect();

  channelSftp.disconnect();

  e.printStackTrace();

  throw new StreamTransformationException(“Exception occured, disconecting.. “+ e.getMessage(), e);

  }

  }

}

To report this post you need to login first.

1 Comment

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

Leave a Reply