Skip to Content
Technical Articles
Author's profile photo Akash Ashok

Connecting to FTP with SAP CPI

SFTP is a file transfer protocol which has made a vast impact across the globe ensuring an secure transfer of data, but there are quite a few(or slightly more than few) customer who are still depending on FTP rather than SFTP.

SAP CPI doesn’t provide us with any standard method to connect to FTP server, so with quite a bit of research and groovy skills (thanks to SAP CPI, i am turning into a Groovy expert) i was able to connect to FTP server with the below code.

With this Groovy code, you would be able to connect to FTP read the file and store the data in body. As a part of testing, i have tried it on file extensions like txt,csv,bin (majorly txt formats) and not word or excel or pdf format’s.

Things you would need to do :

  1. Import the Apache Common Net Jar file in archive
  2. Raise a ticket with SAP CPI Operations insisting them to open the port to communicate with your FTP.
    In general FTP port is 21

Groovy Code:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import org.apache.commons.net.ftp.FTPClient
import org.apache.commons.net.ftp.FTPFile

def Message processData(Message message) {
    //Body 
       def body = message.getBody(java.lang.String)as String;
   
    	FTPClient ftpClient = new FTPClient()
	ftpClient.connect('host name');
	ftpClient.login('user name','password');    
		
	//Get ftp body
	ftpClient.enterLocalPassiveMode() ;
	InputStream inputStream = ftpClient.retrieveFileStream(new String('ftp file name'.getBytes("UTF-8"), "iso-8859-1"));
	ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
	byte[] buff = new byte[100];
	int rc = 0;
	int value =100;
	while ((rc = inputStream.read(buff, 0, value)) > 0) {
	swapStream.write(buff, 0, rc);
	}
	
	body = swapStream.toString();
		
	def messageLog = messageLogFactory.getMessageLog(message);
	messageLog.addAttachmentAsString("log ftp data", body.toString(), "text/plain");
        
        
       return message;
}

 

Apache Common Net jar : https://commons.apache.org/proper/commons-net/download_net.cgi

 

Hope this helps

Assigned tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Binod Mondal
      Binod Mondal

       

      Thanks for sharing. Good to know. 🙂

      Author's profile photo Vivek Manjunath
      Vivek Manjunath

      Thanks for sharing Akash

       

      Author's profile photo Imran Shaik
      Imran Shaik

      Hi Ashok,

      Can you explain about – Import the Apache Common Net Jar file in archive

      Where do we need to import this after downloading. I have requirement to pick the file from FTP and send that to SFTP using SAP CPI. Please guide me on how to connect FTP. Thanks in advance.

       

      Regards,

      Imran

      Author's profile photo s douluri
      s douluri

      Good information. Hi Ashok, Did you achieve your requirement to connect FTP from CPI and viceversa?