Skip to Content
Author's profile photo Former Member

SFTP using custom adapter module Part1 – Receiver

More often we have faced challenges for using SFTP protocol in SAP PI, especially the days when we did not have standard solution for it. Back then we have used workarounds using shell scripts or bought third party adapters. Now that with SP 8.0 and higher service pack for PI 7.11 sap itself has provided standard adapters for SFTP solution (Here), the need of using SFTP protocol in SAP PI has become simpler.

However, Older service packs still have to rely on custom solutions or 3rd party adapters, This blog would stand helpful for older service packs. In this article I will explain how SFTP can be done using adapter module.

For simplicity I created two separate modules, One for sender (to read files from SFTP server) and one for Receiver (to create files on SFTP server). This blog would cover the Receiver Module, I will be creating another blog for the Sender (Here).

To build these modules and to connect SFTP server I have used JSch jcraft jars, mainly because of its various methods and easiness in using them in custom classes. Moreover I found JSCH is capable of handling various authentication methods that are Password based, Public key and Host based. There are lots of example on to use these classes. For instance below is snippet of one of the method(Authenticating with Password and connecting to SFTP server) that I have used in the receiver module.

import com.jcraft.jsch.*;import*;
   { JSch jsch = new JSch(); Session session = jsch.getSession(Username, HostName, Portno);
 session.setConfig("StrictHostKeyChecking", "no");     
Channel channel = session.openChannel("sftp");
ChannelSftp sftpChannel = (ChannelSftp) channel;;
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Connection to SFTP location Successful");
// Creating file with applied filename
String outfname = filename;
if (Outfilename != null) {
    outfname = Outfilename;
if (Timestamp != null) {
    Date date = new Date();
// get the current date
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
// set the format for date
    String dfmt = dateFormatter.format(date);
    dfmt = dfmt + ".";
    outfname = outfname.replace(".", dfmt);
        String toutfname = outfname;
    if (tempfname != null) {
    toutfname = toutfname + ".tmp";
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Writing file with temporary name");
    sftpChannel.put(isXML, toutfname);
    if (tempfname != null) {
    try {
    sftpChannel.rename(toutfname, outfname);
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,
    "File Renaming is succesful");
    } catch (Exception e) {
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Module Exception caught:");
    ModuleException me = new ModuleException(e);
    throw me;
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, outfname + "File written sucessfully in " + Directory);
    // Set content as byte array into payload
    // Sets the principle data that represents usually the message to be
    // processed
        } catch (Exception e) {
            Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,
                    "Module Exception caught:");
            ModuleException me = new ModuleException(e);
            throw me;

Likewise we can do various operations, for example creating the file with temporary name while it is being written and renaming it to final file name once file writing is completed and so on. Once the module code is completed with required operations and is deployed in SAP PI, Next step is to use it. I used it in module section of standard File adapter, with this my interface now will call the custom adapter module to post file on sftp server but additionally it will also call standard processing sequence of File adapter to create a file at file location mentioned in Parameters tab of standard file adapter. On a loner run these additional file, created by standard file adapter if needed can be used as an archive repository of data sent to sftp server or if not we can simply pass empty payload to this additional file for so any data security reason (The module code will have to be modified accordingly to pass empty payload to standard file adapter processing  sequence CallSapAdapter of the module section).

For content conversion we can use standard bean StrictXml2PlainBean and can include in module section of file adapter before our custom sftp module.


And for monitoring, We can add audit logs at every step inside module code and same will be visible in communication channel monitoring logs.

References :

SFTP using custom adapter module Part2 – Sender…

JSch – Java Secure Channel – Examples

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Amol,

      Nice blog. Even I tried in doing it my way which could be helpful to us.

      SFTP Adapter Module in SAP PI 7.0



      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Thanks Ashutosh