2012-06-11 189 views
4

ftpClient.connect與現有主機(未啓用ftp服務)一起使用時,超時僅在5分鐘後發生,這太長了。爲FtpClient設置連接超時

我試着設置不同的超時(setDefaultTimeout,setDataTimeout),它沒有改變任何東西。

FtpClient繼承自SocketClient它有一個setConnectTiemout方法,但是當我使用它時,我得到一個java.lang.NoSuchMethodError: org/apache/commons/net/ftp/FTPClient.setConnectTimeout運行它時。這似乎是因爲一些J2SW 1.2兼容性,如Commons-net FAQ中所述:

問:如何設置連接超時? http://wiki.apache.org/commons/Net/FrequentlyAskedQuestions

他們建議實現自己的SocketFactory使用特定的超時從擴展的Socket類創建對象。但是,當試圖使用ftpClient.setSocketFactory時,我也會得到一個java.lang.NoSuchMethodError

任何幫助我如何減少連接超時?

+2

提到的常見問題解答問題及回答說:'因爲共享網絡的1.2.x有J2SE 1.2的兼容性的要求,不包括指定連接超時的能力。這意味着Commons Net 1.2.x庫不具備所需的功能。你需要專門的舊版本庫嗎?否則,請嘗試更新或者最新版本,例如Commons Net 3.1 - 有'FTPClient'類具有'setConnectTimeout'方法,正如你在下面的答案中提到的那樣完成你所需要的。 – Yura

+0

我使用FTPClient 3.1,我的Eclipse沒有顯示錯誤,但是在應用程序服務器上部署後,它提供了所提到的錯誤。 Mavne build pom.xml具有相同的依賴版本,ear文件包含正確的jar。 – stracktracer

+2

這真的很奇怪,因爲這應該工作...你嘗試從Eclipse下簡單的'公共靜態無效的主要(字符串[] args)'方法連接?也許問題出在應用程序服務器上:它可能會提供舊版本的Commons Net庫,並且您部署的應用程序可能會從那裏使用FTPClient ... – Yura

回答

3

雖然對於較舊版本的Commons Net庫有可能的解決方案,但我建議找出爲什麼使用Commons Net的錯誤版本。要做到這一點,你可以包括下面的代碼到FTPClient在你的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 
} 

在情況下,如果FTPClient不加載由java.net.URLClassLoader那麼它可能變得更加複雜,檢查類路徑,但它不應該成爲一個問題。

希望這有助於...

+0

該數組由14個條目組成:weblogic/patch_wls1032/profiles/default/sys_manifest_classpath/weblogic_patch.jar,weblogic/jrmc-4.0.1-1.6.0/lib/tools.jar,weblogic/utils/config/10.3/config- launch.jar,weblogic/wlserver_10.3/server/lib/weblogic_sp.jar,weblogic/wlserver_10.3/server/lib/weblogic.jar,...不包括我的ear-deployment中捆綁的commons-net庫。 – stracktracer

+2

嗯,這很奇怪。從未使用過weblogic,但它看起來像下面的問題/答案接近您所需: http://stackoverflow.com/questions/3376046/weblogic-10-3-1-0-is-using-com-bea -core-apache-commons-net-1-0-0-0-1-4-1-jar – Yura

+0

確實,一些具體的工作是爲classloader設置衝突類的優先級,所以捆綁的類首先被加載。 – Alfabravo

1

它必須以您調用setConnectTimeout的方式進行,因爲它確實存在。 setConnectTimeout不是靜態調用,您必須在分配FTPClient對象並在連接之前執行設置後調用它。

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

這正是我所做的。 – stracktracer

+0

必須是您的設置。我使用的是commons-net-3.0.1.jar。我在eclipse中試過,只是爲了確保。您可能需要檢查並確保沒有先裝入較舊的jar文件。 – Mike

1
FTPClient ftp = new FTPClient(); 

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

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. 
    * <p> 
    * @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 &ge; 0. 
    * <p> 
    * <b>Note:</b> 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()}. 
    * <p> 
    * 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)