Java
Chilkat2 Java
Set FTP Connection and Read Timeouts
See more FTP Examples
Demonstrates the timeout properties ConnectTimeout, ReadTimeout, and IdleTimeoutMs.
Background: The three timeouts guard different phases:
ConnectTimeout (in seconds) bounds how long a connection attempt blocks so an unreachable server fails promptly; ReadTimeout (seconds) limits how long a data connection may stall without new data; and IdleTimeoutMs (milliseconds) caps a lack of forward progress overall. Tuning them prevents a hung server or flaky link from freezing an automated job indefinitely.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Ftp2;
public class ChilkatExample {
public static void main(String[] args) {
Ftp2 ftp = new Ftp2();
ftp.setHostname("ftp.example.com");
ftp.setUsername("myFtpLogin");
// Maximum seconds to wait for the TCP connection to be accepted.
ftp.setConnectTimeout(15);
// Maximum seconds a data connection may go without receiving more data before timing out.
ftp.setReadTimeout(30);
// Maximum milliseconds without a control-channel response or forward progress before failing.
ftp.setIdleTimeoutMs(20000);
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
ftp.setPassword("myPassword");
try {
ftp.connect();
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Connected with custom timeouts.");
try {
ftp.disconnect();
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
}
}