Jsawyer
Jsawyer

Reputation: 73

Connect to Localhost with Java FTP

I'm developing FTP program on JAVA. I'm using Apache Commons Net Library. My Codes are below.

import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class ServerClass {
    private static void showServerReply(FTPClient ftpClient) {
        String[] replies = ftpClient.getReplyStrings();
        if (replies != null && replies.length > 0) {
            for (String aReply : replies) {
                System.out.println("SERVER: " + aReply);
            }
        }
    }
    public static void main(String[] args) {
        String server = "127.0.0.1";
        int port = 80;
        String user = "root";
        String pass = "root";
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(server, port);
            showServerReply(ftpClient);
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                System.out.println("Operation failed. Server reply code: " + replyCode);
                return;
            }
            boolean success = ftpClient.login(user, pass);
            showServerReply(ftpClient);
            if (!success) {
                System.out.println("Could not login to the server");
                return;
            } else {
                System.out.println("LOGGED IN SERVER");
            }
        } catch (IOException ex) {
            System.out.println("Oops! Something wrong happened");
            ex.printStackTrace();
        }
    }
}

But I can't connect my localhost. I want to login my localhost and see my file. My errors are below.

Oops! Something wrong happened
java.net.ConnectException: Connection refused: connect
	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
	at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
	at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
	at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
	at java.net.PlainSocketImpl.connect(Unknown Source)
	at java.net.SocksSocketImpl.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at org.apache.commons.net.SocketClient.connect(SocketClient.java:188)
	at org.apache.commons.net.SocketClient.connect(SocketClient.java:209)
	at com.emrecanoztas.ftp.ServerClass.main(ServerClass.java:22)

can you help me anybody? Thanks!..

Upvotes: 0

Views: 3878

Answers (2)

Marcin Krasowski
Marcin Krasowski

Reputation: 686

Try the settings below.

        String server = "ftp.icm.edu.pl";
        int port = 21;
        String user = "anonymous";
        String pass = "[email protected]";

Upvotes: 0

Konrad
Konrad

Reputation: 355

Questions:

  • Is there an FTP server running?
  • Is there a message in the FTP server log about the connect request?
  • Does the FTP server allow connections from localhost?
  • Is the FTP server listening on localhost or should you use the public IP/name of the computer? (check with netstat)

Upvotes: 1

Related Questions