I have Solved this problem using Cyberduck library.
https://github.com/iterate-ch/cyberduck
first why this Handshake problem arises,
because some advances FTP servers allow only one session for connection and data transmission.
1)Control Session - > for connection
2)Data Session - > data storage/download/etc
So how to solve this.
Step 1 -:
First You need to checkout cyberduck repo from GitHub.
from here - : https://github.com/iterate-ch/cyberduck
Step 2 -: You will see FTP and core module in checkout repo
ftp and core modules
Step 3 -: Convert this module into maven and create jar for the same.
Step 4 -: add this jar into you project, but this jar also requires other dependencies so add those accordingly into project build path as well.
Step 5 -: Code snippet.
public class TestFTPS {
public static void main(String[] args) throws SocketException, IOException {
TrustManager[] trustManagers = new TrustManager[] { new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// TODO Auto-generated method stub
}
} };
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustManagers, new SecureRandom());
} catch (Exception e) {
e.printStackTrace();
}
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
Protocol protocol = new FTPTLSProtocol();
FTPClient client = new FTPClient(protocol, sslSocketFactory, sslContext);
client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
client.connect(ftphostname, port);
client.execAUTH("TLS"); //SSL
System.out.println(client.getReplyCode());
if (client.login(username, password)) {
client.execPBSZ(0);
client.execPROT("P");
String date = "Testing Data Send to provide FTP location";
client.setFileType(FTP.BINARY_FILE_TYPE);
client.enterLocalPassiveMode();
InputStream is = new ByteArrayInputStream(date.getBytes());
client.storeFile("test.txt", is);
System.out.print(client.getReplyCode());
}
}
}
Step 6 -: After running this code, your file will be transferred successfully to ftp location.
Note - :Please add required jar in your project
Hope This will help you.