-2

I am trying to connect to multiple ftp sites ,the thing is only the ftp host name (ip address) varies between different ftp sites and the username,password,port ,directory are all the same where i would to download and read /retrieve the latest files.

I have an existing flow,where it connects to one particular ftp site and does the operation,now i want to add multiple ftp sites using the context variable and invoke multiple flows for different ftp sites as part of the same flow considering only the HOSTNAME will vary,everything else is exactly the same.

Should i use a trunjob component or which is the easiest way to handle this?enter image description here

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Tommy
  • 55
  • 1
  • 6

1 Answers1

0

Have you thought of using SFTP? Here's a snippet using JSch library, you can modify it for multiple connections:

private static final String SFTP_SERVER_URL = "server.com";
private static final String SFTP_SERVER_USERNAME = "user";
private static final String SFTP_SERVER_PASSWORD = "pass";
private static final int SFTP_SERVER_PORT = port;
private final static String LOCAL_DOWNLOAD_PATH = "downloads/";

private static ChannelSftp setupSFTP() {
    ChannelSftp channel = null;
    try {
        JSch jsch = new JSch();
        JSch.setConfig("StrictHostKeyChecking", "no");
        Session jschSession = jsch.getSession(SFTP_SERVER_USERNAME, SFTP_SERVER_URL);
        jschSession.setPassword(SFTP_SERVER_PASSWORD);
        jschSession.setPort(SFTP_SERVER_PORT);
        jschSession.connect();
        channel = (ChannelSftp) jschSession.openChannel("sftp");
    } catch (JSchException e) {
        e.printStackTrace();
    }
    return channel;
}

public static void downloadFile(String path, String fileName) {
    try {
        ChannelSftp channelSftp = setupSFTP();
        channelSftp.connect();

        // Download file and close connection
        channelSftp.get(path, LOCAL_DOWNLOAD_PATH + fileName);
        channelSftp.exit();
    } catch (SftpException e) {
        e.printStackTrace();
    } catch (JSchException e) {
        e.printStackTrace();
    }
}

public static void uploadFile(String localPath, String remotePath) {
    try {
        ChannelSftp channelSftp = setupSFTP();
        channelSftp.connect();

        // Upload file and close connection
        channelSftp.put(localPath, remotePath);
        channelSftp.exit();
    } catch (SftpException e) {
        e.printStackTrace();
    } catch (JSchException e) {
        e.printStackTrace();
    }
}

You can have multiple setup functions, one for each of the servers.

Link: http://www.jcraft.com/jsch/