简体   繁体   English

使用java.net在Java中创建FTP客户端-拒绝连接:connect

[英]Creating FTP Client in Java with java.net -Connection refused: connect

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class FTPClient {
protected Socket sk;
protected BufferedReader in;
protected BufferedWriter out;

public static void main (String [] args){
    FTPClient fc1 = new FTPClient("sitename.org",21);
    fc1.Login("user", "password!");
}

FTPClient(String server, int port){
    try{
        this.sk = new Socket(server,port);
        this.in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
        this.out = new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));

        String response = in.readLine();
        System.out.println(response);
    }
    catch(Exception e){
        System.out.println(e);
    }

}

public boolean Login(String user, String pass){
    boolean success = false;
    try{
        sendOut("USER " + user);
        sendOut("PASS " + pass);
        success = true;
        System.out.println("Waiting for response");
        String response = in.readLine();
        System.out.println(response);

    }
    catch(Exception e){
        System.out.println("Login Failure");
        success = false;
    }

    return(success);
}

public void sendOut(String command) throws Exception{
    if (sk == null){
        throw new Exception("Client is not connected!");
    }

    try{
        out.write(command + "\r\n");
        out.flush();
    }
    catch(Exception e){
        System.out.println(e);
    }
}

} }

Hello, I wrote this code as a client to try to connect and login to a server with an FTP connection. 您好,我作为客户端编写了此代码,以尝试通过FTP连接连接并登录到服务器。 However, I keep getting this error message, java.net.ConnectException: Connection refused: connect Can someone please help me? 但是,我一直收到此错误消息java.net.ConnectException:连接被拒绝:connect有人可以帮帮我吗?

You try to connect to a SSH server, hence you get the unexpected reply. 您尝试连接到SSH服务器,因此收到意外的答复。 The defualt FTP port is 21. 默认的FTP端口是21。

Ok... My School's server doesn't have FTP set up and opened on port 21. So, I started FTP on my server on port 21 and it properly connect now. 好的...我学校的服务器没有设置FTP并在端口21上打开。因此,我在端口21的服务器上启动了FTP,现在可以正确连接了。 Thanks for helping me trouble shoot guys. 感谢您帮助我排除麻烦。

Lessons learned: FTP is normally on port 21. However, if there are connection problems, one should check if the server is even listening on port 21 by logging on and using the "sudo netstat netstat -lntu" command. 经验教训:FTP通常位于端口21上。但是,如果存在连接问题,应通过登录并使用“ sudo netstat netstat -lntu”命令来检查服务器是否还在监听端口21。 If FTP is not even on, one can install it by running "sudo apt-get install vsftpd". 如果FTP甚至没有打开,则可以通过运行“ sudo apt-get install vsftpd”进行安装。

Thanks everyone for helping me troubleshoot to the answer. 感谢大家帮助我解决问题的答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Rserve重置Java.net连接 - Java.net Connection Reset using Rserve Python Server / Java客户端“连接被拒绝:连接” - Python Server / Java Client “Connection refused: connect” Quarkus 测试 rest 客户端 java.net.ConnectException:连接被拒绝:连接 - Quarkus test rest client java.net.ConnectException: Connection refused: connect Java Mail错误:java.net.ConnectException:连接被拒绝:connect - Java Mail Error : java.net.ConnectException: Connection refused: connect Java套接字 - java.net.ConnectException:连接被拒绝:连接 - Java sockets - java.net.ConnectException: Connection refused: connect java.net.ConnectException:连接被拒绝:client / server tcp socket中的connect ,, error - java.net.ConnectException: Connection refused: connect ,,error in client /server tcp socket com.sun.jersey.api.client.ClientHandlerException:java.net.ConnectException:连接被拒绝:在 Spring Boot 中连接 - com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect in Spring Boot Java网络问题:java.net.ConnectException:连接被拒绝:连接 - Java Networking Problem: java.net.ConnectException: Connection refused: connect Java 中的 ssh 异常:java.net.ConnectException:连接被拒绝:连接 - ssh Exception in Java: java.net.ConnectException: Connection refused: connect java.net.ConnectException:连接被拒绝:connect java nio - java.net.ConnectException: Connection refused: connect java nio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM