簡體   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);
    }
}

}

您好,我作為客戶端編寫了此代碼,以嘗試通過FTP連接連接並登錄到服務器。 但是,我一直收到此錯誤消息java.net.ConnectException:連接被拒絕:connect有人可以幫幫我嗎?

您嘗試連接到SSH服務器,因此收到意外的答復。 默認的FTP端口是21。

好的...我學校的服務器沒有設置FTP並在端口21上打開。因此,我在端口21的服務器上啟動了FTP,現在可以正確連接了。 感謝您幫助我排除麻煩。

經驗教訓:FTP通常位於端口21上。但是,如果存在連接問題,應通過登錄並使用“ sudo netstat netstat -lntu”命令來檢查服務器是否還在監聽端口21。 如果FTP甚至沒有打開,則可以通過運行“ sudo apt-get install vsftpd”進行安裝。

感謝大家幫助我解決問題的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM