2011-02-23 75 views
1

我有一個奇怪的問題,我的本地測試設置簡單的Java URL FTP連接。 下面的代碼片段(除去嘗試/捕獲):Java與FileZilla的FTP連接問題

URL url = new URL("ftp://127.0.0.1/subOne/subTwo/subThree/subFour"); 
URLConnection conn = url.openConnection(); 
conn.setConnectTimeout(30000); 
conn.setReadTimeout(30000); 

InputStream is = conn.getInputStream(); /// And here flies the IOException! 

...實際IOException異常,原因是「子一/子二/子三/子四」,但有趣的事情發生在服務器端:

(000012)23.02.2011 13:01:05 - (not logged in) (127.0.0.1)> Connected, sending welcome message... 
(000012)23.02.2011 13:01:05 - (not logged in) (127.0.0.1)> 220 Blabla 
(000012)23.02.2011 13:01:05 - (not logged in) (127.0.0.1)> USER anonymous 
(000012)23.02.2011 13:01:05 - (not logged in) (127.0.0.1)> 331 Password required for anonymous 
(000012)23.02.2011 13:01:05 - (not logged in) (127.0.0.1)> PASS ************* 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 230 Logged on 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> TYPE I 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 200 Type set to I 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> CWD das 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 250 CWD successful. "/subOne" is current directory. 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> CWD 2011 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 250 CWD successful. "/subOne/subTwo" is current directory. 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> CWD 02 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 250 CWD successful. "/subOne/subTwo/subThree" is current directory. 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> EPSV ALL 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 229 Entering Extended Passive Mode (|||3881|) 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> EPSV 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 229 Entering Extended Passive Mode (|||3882|) 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> RETR subFour 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 550 File not found 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> CWD subOne 
(000012)23.02.2011 13:01:05 - anonymous (127.0.0.1)> 550 CWD failed. "/subOne/subTwo/subThree/subOne": directory not found. 
(000012)23.02.2011 13:03:06 - anonymous (127.0.0.1)> 421 Connection timed out. 
(000012)23.02.2011 13:03:06 - anonymous (127.0.0.1)> disconnected. 

我不明白,因何測試儀試圖進入擴展被動模式和爲什麼它添加子一後不能取回子四

我剛剛安裝了FileZilla Server並設置了匿名用戶和共享驅動器。我通過瀏覽器和FileZilla-Client檢查了FTP-Dir是否可以訪問(當然是登錄),就是這樣。 一切都安裝在同一臺機器上運行!

不知道任何進一步的...

感謝您的幫助!

回答

4

這種方式連接到FTP是相當有限和模糊記錄。我可以先給你答覆EPSV。該連接由內部實現建立,在我的JDK中恰好是sun.net.www.protocol.ftp.FtpURLConnection

當連接到服務器的第一EPSVPASV將嘗試(默認爲被動模式),那麼它會回落到活動模式 - PORT - 如果被動模式不能被建立。你可以看到實施細節here

基本解釋評論你的問題之一是:

/** 
    * Here is the idea: 
    * 
    * - First we want to try the new (and IPv6 compatible) EPSV command 
    * But since we want to be nice with NAT software, we'll issue the 
    * EPSV ALL cmd first. 
    * EPSV is documented in RFC2428 
    * - If EPSV fails, then we fall back to the older, yet OK PASV command 
    * - If PASV fails as well, then we throw an exception and the calling method 
    * will have to try the EPRT or PORT command 
    */ 

關於你的第二個問題... 子四的失敗檢索...好吧,先快速瀏覽一下,似乎是表現方式,因爲它是越野車。但我現在無法真正安裝適當的環境來驗證。此外,你有例外。我猜想當它試圖再次導航到完整路徑時,問題在第455行開始。 FTP連接的完整來源是here

373  public InputStream getInputStream() throws IOException { 
    ... 
    390   try { 
    391    decodePath(url.getPath()); 
    392    if (filename == null || type == DIR) { 
    ... 
    399    } else { 
    400     if (type == ASCII) 
    401      ftp.ascii(); 
    402     else 
    403      ftp.binary(); 
    404     cd(pathname); 
    405     is = new FtpInputStream(ftp, ftp.get(filename)); 
    406    } 
    407 
    408 
    ... 
    453   } catch (FileNotFoundException e) { 
    454    try { 
    455     cd(fullpath); 
    456     /* if that worked, then make a directory listing 
    457      and build an html stream with all the files in 
    458      the directory */ 
    459     ftp.ascii(); 
    460 
    461     is = new FtpInputStream(ftp, ftp.list()); 
    462     msgh.add("content-type", "text/plain"); 
    463    } catch (IOException ex) { 
    464     throw new FileNotFoundException(fullpath); 
    465    } 
    466   } 
    ... 
    469  } 

我會建議你使用Apache Commons Net庫FTP操作。它使用起來更加先進和直接。

如果您喜歡,歡呼和快樂調試!

+0

很好的答案,@lucho。 – andersoj 2011-02-23 13:42:56

+0

感謝您的時間和答案!我擔心這個可能會深埋在JDK中以解決簡單的問題...我將嘗試Apache Commons ...再次感謝! – Gruber 2011-02-23 14:11:33