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/