2015-04-12 91 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

相关问题