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:连接被拒绝:连接可以有人帮助我吗?
发布于 2015-10-18 17:32:46
您尝试连接到SSH服务器,因此得到意外的回复。默认FTP端口为21。
发布于 2015-10-18 18:06:19
好的..。我学校的服务器没有在端口21上设置和打开FTP。所以,我在我的服务器上的端口21上启动了FTP,它现在可以正常连接了。谢谢你帮我解决问题。
经验教训: FTP通常在端口21上。但是,如果存在连接问题,应该通过登录并使用"sudo netstat netstat -lntu“命令来检查服务器是否正在侦听端口21。如果FTP甚至没有打开,可以通过运行"sudo apt-get install vsftpd“来安装它。
感谢大家帮我排查问题的答案。
https://stackoverflow.com/questions/33196348
复制相似问题