2015-04-12 163 views
0

我製作了一個不能連接到服務器的FTP客戶端(被動)。我使用的FTP服務器是Filezilla;我只是用它進行測試。我每次運行Java程序(FTP客戶端)FileZilla中斷開,我在Eclipse中得到這些錯誤:Exception in thread "main" java.io.IOException: SimpleFTP received an unknown response when connecting to the FTP server: 220-FileZilla Server version 0.9.50 beta at ftp.ftp.connect(ftp.java:25) at ftp.test.main(test.java:12)爲什麼我無法將我的FTP客戶端連接到服務器?

這是FTP客戶端:

package ftp; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.Socket; 

public class ftp 
{ 

public synchronized void connect(String host, int port, String user, 
      String pass) throws IOException { 
    Socket socket = new Socket(host, port); 
    // if (socket != null) { 
    //  throw new IOException("SimpleFTP is already connected. Disconnect first."); 
    // } 
     // socket = new Socket(host, port); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
     BufferedWriter writer = new BufferedWriter(
      new OutputStreamWriter(socket.getOutputStream())); 

     String response = reader.readLine(); 
     if (!response.startsWith("220-")) { 
      throw new IOException(
       "SimpleFTP received an unknown response when connecting to the FTP server: " 
        + response); 
     } else System.out.println("1:"+response); 

     writer.write("user geek"+"\r\n"); 
     writer.flush(); 
     response= reader.readLine(); 
     System.out.println("2"+response); 
     // response = reader.readLine(); 
     /* if (!response.startsWith("331 ")) { 
      throw new IOException(
       "SimpleFTP received an unknown response after sending the user: " 
        + response); 
     } 
     else {*/ 
     writer.write("PASS hello" +"\r\n"); 
     writer.flush(); 
      response= reader.readLine(); 
      System.out.println("3"+response); 
     //} 


     response = reader.readLine(); 
     /*if (!response.startsWith("230 ")) { 
      throw new IOException(
       "SimpleFTP was unable to log in with the supplied password: " 
        + response); 
     } 
     else {*/ 
      System.out.println("4"+response); 
     //} 
} 

} 

而這正是我連接程序:

package ftp; 

import java.util.Scanner; 

public class test { 
private static Scanner scan; 

public static void main(String args[]) throws Exception 
    { 

     ftp s = new ftp(); 
      s.connect("localhost",21,"geek", "hello"); 

    } 

} 

也試圖寫我的局域網IP,而不是"localhost"

+0

如果你在客戶端使用'URLConnection',你可以簡化你的代碼。有關詳細信息,請參閱[此答案](http://stackoverflow.com/a/29542935/905488)。這樣你就不需要手動搞定套接字了。 –

回答

2

... 220-的FileZilla Server版本0.9.50 Beta版

if (!response.startsWith("220 ")) { 

你期待與220<space>開始,而服務器發送你開始220-一個響應的響應。請閱讀standard以瞭解如何處理多行回覆。

+0

這是正確:)爲什麼說:'1:220-FileZilla服務器版本0.9.50測試版 2220 - 由Tim Kosse編寫([email protected]) 3220請訪問https:// filezilla- project.org/ 4331極客需要的密碼 '在filezilla中設置了密碼hello in – Hodhod

+0

爲什麼不應該這麼說?實際上,對於FTP服務器來說,最初的歡迎消息由很多行組成,通常包括一個使用策略等等,這是非常普遍的。 –

+0

我的意思是說「密碼需要爲極客」,即使我已將密碼設置爲「hello」 – Hodhod

相關問題