【问题标题】:Why can't I connect my FTP client to the server?为什么我的 FTP 客户端无法连接到服务器?
【发布时间】:2015-04-12 15:19:55
【问题描述】:

我创建了一个无法连接到服务器的 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 客户端:

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"

【问题讨论】:

  • 如果您在客户端中使用URLConnection,您可以大大简化您的代码。有关详细信息,请参阅this answer。这样你就不需要手动弄乱套接字了。

标签: java ftp


【解决方案1】:

... 220-FileZilla 服务器版本 0.9.50 beta

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

您期望以220<space> 开头的响应,而服务器向您发送以220- 开头的响应。请阅读standard 了解如何处理多行回复。

【讨论】:

  • 没错:) 为什么会说:1:220-FileZilla Server version 0.9.50 beta 2220-written by Tim Kosse (tim.kosse@filezilla-project.org) 3220 Please visit https://filezilla-project.org/ 4331 Password required for geek 已经在filezilla中设置了密码为hello
  • 为什么不应该这么说呢?对于 FTP 服务器来说,最初的欢迎消息实际上很常见,它由很多行组成,通常包括使用策略等。
  • 我的意思是说“极客需要密码”,即使我已将密码设置为“你好”
  • 331 Password required 是对您的user geek 命令的响应。我认为你真的应该阅读标准。
猜你喜欢
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 2012-10-15
  • 2022-01-15
相关资源
最近更新 更多