I am getting a SocketTimeoutException while uploading a file. Retrieve file works fine for 2 files but then it fails on uploading different 2 files. The exception occurs during first file upload. This only happens in the Cloud. The code works fine on my local machine.
In stack trace it looks like keep-alive reply timeout(3 sec) throw this. How to setup FTP Timeout to fix this? Will increasing keep-alive reply timeout be sufficient? Or decrease keep-alive timeout?
Documentation:
https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html
Simplified code
ftp = new FTPClient();
ftp.addProtocolCommandListener(newPrintCommandListener(newPrintWriter(System.out),true));
ftp.setControlKeepAliveTimeout(Duration.ofSeconds(20));
ftp.setConnectTimeout(7200000);
ftp.setDefaultTimeout(7200000);
ftp.setControlKeepAliveReplyTimeout(3000);
ftp.setBufferSize(1024*1024);
ftp.setDefaultPort(21);
InetAddresslocalAddr = InetAddress.getLocalHost();
int localPort = 40044;
ftp.connect(host,port,localAddr,localPort);
int reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)){
ftp.disconnect();
throw new IOException("Exception in connecting to FTPServer");
}
ftp.enterLocalPassiveMode();
boolean loginResult = ftp.login(user,pass);
if(!loginResult){
throw new Exception("FTP login failed");
}
ftp.setFileType(FTP.ASCII_FILE_TYPE);
ftp.setKeepAlive(true);
try {
FileOutputStream fos1 = new FileOutputStream(file1.toString());
FileOutputStream fos2 = new FileOutputStream(file2.toString());
ftp.retrieveFile(remote1, fos1);//works fine
ftp.retrieveFile(remote2, fos2);//works fine
fos1.close();
fos2.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
try {
FileInputStream fis1 = new FileInputStream(file3);
ftp.storeFile(remote3, fis1); //throws exception
fis1.close();
FileInputStream fis2 = new FileInputStream(file4);
ftp.storeFile(remote4, fis2);
fis2.close();
} catch (Exception e) {
throw new RuntimeException(e);
}