Sample code for 30+ languages & platforms
Java

Quote and SendCommand

See more FTP Examples

Demonstrate the Quote and SendCommand methods.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Ftp2;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Ftp2 ftp = new Ftp2();

        ftp.setHostname("ftp.example.com");
        ftp.setUsername("login");
        ftp.setPassword("password");

        // Connect and login to the FTP server.
        try {
            ftp.connect();

            // Tell the FTP object to keep an in-memory session log
            // so we can see the commands sent to the server,
            // and the responses received back.
            ftp.setKeepSessionLog(true);

            // Change the current remote directory via the Quote method:
            ftp.quote("CWD junk");

            // Move back up
            // In this case, ChangeRemoteDir sends "CWD .." to the FTP server.
            ftp.changeRemoteDir("..");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Do the same via the SendCommand method where the
        // raw FTP server response is returned:
        String serverResponse = null;
        try {
            serverResponse = ftp.sendCommand("CWD junk");
            System.out.println(serverResponse);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
        }

        ftp.disconnect();

        System.out.println("Session Log:");
        System.out.println(ftp.getSessionLog());
    }
}