Saltar al contenido

Configuración del tiempo de espera de conexión para FtpClient

Si encuentras algún fallo con tu código o trabajo, recuerda probar siempre en un ambiente de testing antes aplicar el código al trabajo final.

Solución:

    FTPClient ftp = new FTPClient();

    ftp.setDefaultTimeout();
    ftp.setDataTimeout();
    ftp.setConnectTimeout();
    ftp.setSoTimeout();
    ftp.setControlKeepAliveTimeout();
    ftp.setControlKeepAliveReplyTimeout();

De los documentos de Apache:

   /**
     * Set the default timeout in milliseconds to use when opening a socket.
     * This value is only used previous to a call to
     * @link #connect connect()
     * and should not be confused with @link #setSoTimeout setSoTimeout()
     * which operates on an the currently opened socket.  _timeout_ contains
     * the new timeout value.
     * 

* @param timeout The timeout in milliseconds to use for the socket * connection. */ void setDefaultTimeout(int timeout); /** * Sets the timeout in milliseconds to use when reading from the * data connection. This timeout will be set immediately after * opening the data connection, provided that the value is ≥ 0. *

* Note: the timeout will also be applied when calling accept() * whilst establishing an active local data connection. * @param timeout The default timeout in milliseconds that is used when * opening a data connection socket. The value 0 means an infinite timeout. */ void setDataTimeout(int timeout) /** * Sets the connection timeout in milliseconds, which will be passed to the @link java.net.Socket object's * connect() method. * @param connectTimeout The connection timeout to use (in ms) * @since 2.0 */ void setConnectTimeout(int connectTimeout); /** * Set the timeout in milliseconds of a currently open connection. * Only call this method after a connection has been opened * by @link #connect connect(). *

* To set the initial timeout, use @link #setDefaultTimeout(int) instead. * * @param timeout The timeout in milliseconds to use for the currently * open socket connection. * @exception SocketException If the operation fails. * @throws NullPointerException if the socket is not currently open */ void setSoTimeout(int timeout) throws SocketException; /** * Set the time to wait between sending control connection keepalive messages * when processing file upload or download. * * @param controlIdle the wait (in secs) between keepalive messages. Zero (or less) disables. * @since 3.0 * @see #setControlKeepAliveReplyTimeout(int) */ void setControlKeepAliveTimeout(long controlIdle); /** * Set how long to wait for control keep-alive message replies. * * @param timeout number of milliseconds to wait (defaults to 1000) * @since 3.0 * @see #setControlKeepAliveTimeout(long) */ void setControlKeepAliveReplyTimeout(int timeout)

Debe estar en la forma en que llama a setConnectTimeout, porque existe. setConnectTimeout no es un static call, debe llamarlo después de asignar el objeto FTPClient y hacer el conjunto antes de la conexión.

FTPClient ftp = new FTPClient();
ftp.setConnectTimeout(5000); // 5000 Milliseconds (5 Seconds)
... 
ftp.connect(server, port); 

Aunque existe una posible solución para esa versión anterior de la biblioteca Commons Net, sugiero averiguar por qué se usa una versión incorrecta de Commons Net. Para hacer esto, puede incluir el siguiente código en el lugar donde FTPClient se utiliza en su aplicación web:

FTPClient ftpClient = ...;
if(ftpClient.getClass().getClassLoader() instanceof java.net.URLClassLoader) 
    URL[] urls = ((java.net.URLClassLoader) ftpClient.getClass().getClassLoader()).getURLs();
    // log these urls somewhere and check - these are urls from where your FTPClient may be loaded from

en caso de que si FTPClient no está cargado por java.net.URLClassLoader entonces puede ser más complicado verificar el classpath, aunque no debería ser un problema.

Espero que esto ayude…

Puntuaciones y comentarios

Recuerda que te brindamos la opción de añadir una estimación verdadera si te fue útil.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *