1

I got stuck with FTP client java implementation. All I have to do is to connect to the FTP server and transfer one file to the server.

I managed to make a connection with the ftp server (see code below) and after that I entered passive mode with PASV command. Now I don't know what to do next. I tried to make a new socket after PASV command and to connect it to the FTP server port 20 but that didn't work.

My question is how to initiate file transfer when a connection is made? (My idea is to make a connection with port 20 and to execute STOR command, but I don't know how to do it).

Do you have any ideas or helpful advice?

btw. I have to implement this without using java classes like FTPClient

Here's my code:

   public class FTPtest {

    Socket socket;
    PrintWriter pw;
    BufferedReader input;
    String info = "";

    public FTPtest(){
        try{
            socket = new Socket("some_ftp_server", 21);
            logUsername();
            closeEverything();
        }
        catch(IOException ioe){
            System.out.println("error");
        }
    }

    public void logUsername()throws IOException{
        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        pw = new PrintWriter(socket.getOutputStream());

        //user
        pw.write("USER some_user\n");
        pw.flush();
        System.out.println(input.readLine());

        //pass
        pw.write("PASS some_pass");
        pw.flush();
        System.out.println(input.readLine());
        System.out.println(input.readLine());

        //PASV
        pw.write("PASV");
        pw.flush();
    }

    public void closeEverything() throws IOException{
        input.close();
        pw.close();
        socket.close();
    }
    public static void main(String[]args){
        new FTPtest();
    }
}
2
  • Do you want to do a FTP Client yourself? Because if you are just searching for a way to connect to an ftp server and do some operations you can take a look at Apache Commons-Net which has an integrated FTP Client class Commented Apr 10, 2013 at 15:38
  • I want to do FTP Client myself without using any FTP client classes. Commented Apr 10, 2013 at 15:43

2 Answers 2

1

You'd better use already existent FTP client library. As for PASV request - server in answer to it returnes encoded IP address and port to which you should connect for data connection.

Sign up to request clarification or add additional context in comments.

2 Comments

I'd like to use FTP client library but my task is to do it without using any.
In such case you'll need to spend some time on reading RFC 959
0

I've bumped into the same problem. Reading RFC 959 I noticed:

The argument field consists of a variable length character string ending with the character sequence (Carriage Return, Line Feed) for NVT-ASCII representation; for other negotiated languages a different end of line character might be used. It should be noted that the server is to take no action until the end of line code is received.

Instead of

pw.write("USER some_user\n");

try

pw.write("USER some_user\r\n");

worked for me...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.