[英]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.