One of the recurring inquiries in SAP Sourcing has been the use of FTP within scripts. While this might be a simple task in Java, the biggest problem is that Sourcing 9.x doesn't use the all-too-familiar Apache Commons FTPClient.
In this blog post I will explain how to make use of the internal functions available in SAP Sourcing 9.x to facilitate working with FTP folders.
**WARNING**- this blog post will make use of Internal APIs. Even if I have tested and confirmed them working in 9.x versions, great care should be taken while implementing and supporting these APIs.
- Prerequisites:
- Check and confirm that your FTP integration System Properties are defined and access is granted to respective directory.
The fun part: scripting
import java.io.*;
import java.util.*;
import com.sap.eso.sapintegration.util.SapIntUtil;
import com.sap.odp.doc.integration.IntegrationConfig;
//CONSTANTS
String FTP_SERVER = "10.10.10.10";
String FTP_USER = "MyFtpUsername";
String FTP_PASSWORD = "MyFtpPassword";
String FTP_OUTBOUND_DIRECTORY = "my/outbound/directory";
- Prepare your file
- Let's assume you have an AttachmentIfc you want to push to FTP location
File exportFile = attachmentIfc.getFileData(session);
- Or maybe kushagra.agrawal needs to create a file from a String :smile:
String xmlString = "sample XML text";
tempDirectory = IntegrationConfig.getEnterpriseProperty(session, "ftp.temp_dir");
File exportFile = new File(tempDirectory + File.separator + "my_file.xml");
writer = new OutputStreamWriter(new FileOutputStream(exportFile), "UTF-8");
writer.write(xmlString);
writer.close();
- Finally, write your File to FTP
SapIntUtil.instance().ftpFile(session, exportFile.getParent(), FTP_OUTBOUND_DIRECTORY, FTP_SERVER, FTP_USER, FTP_PASSWORD, exportFile.getName());
**PS** For SAP Sourcing 10.x a different post will follow because the changes introduced allow for a better implementation with the use of File Transfer Configuration
Bogdan Toma