1
votes

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();
    }
}
3
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 - BackSlash
I want to do FTP Client myself without using any FTP client classes. - Branko

3 Answers

1
votes

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.

0
votes

Please review RFC 959 for syntax and response details, implementing a full FTP client in Java is not trivial and will take considerable time. Consider using JSCAPE's Secure FTP Factory for a robust implementation of FTP/FTPs protocols. Here is the documentation showing example code.

0
votes

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...