Example usage for org.apache.commons.net.ftp FTPClient pwd

List of usage examples for org.apache.commons.net.ftp FTPClient pwd

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPClient pwd.

Prototype

public int pwd() throws IOException 

Source Link

Document

A convenience method to send the FTP PWD command to the server, receive the reply, and return the reply code.

Usage

From source file:net.paissad.jcamstream.utils.FTPUtils.java

public void estabishConnection() throws SocketException, IOException, FTPException {

    this.setFtpClient(new FTPClient());
    String errMsg;/* w  w  w  .  j av  a2s  .com*/

    FTPClient client = this.getFtpClient();
    PrintCommandListener listener = new PrintCommandListener(System.out);
    client.addProtocolCommandListener(listener);

    // Connects to the FTP server
    String host = this.getFtpServerHost();
    int port = this.getFtpServerPort();
    client.connect(host, port);
    if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
        client.disconnect();
        errMsg = "Unable to connect to the server " + this.getFtpServerHost();
        this.verifyReplyCode(errMsg);
    }

    // Login to the FTP server
    String username = this.getFtpUser();
    String pass = this.getFtpPassword();
    if (!client.login(username, pass)) {
        errMsg = "Unable to login to " + this.getFtpServerHost();
        this.verifyReplyCode(errMsg);
    }

    // Change the current directory
    String dirname = this.getFtpServerDir();
    if (!client.changeWorkingDirectory(dirname)) {

        System.out.println("Unable to change to the directory '" + dirname + "'.");
        System.out.println("Going to create the directory !");
        this.mkdirs(dirname);
        System.out.println("Creation of the directory is successful.");
    }

    client.changeWorkingDirectory(dirname);
    if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
        errMsg = "Unable to change to the directory : " + dirname;
        this.verifyReplyCode(errMsg);
    }

    client.pwd();
}

From source file:org.alfresco.filesys.FTPServerTest.java

/**
 * Test CWD for FTP server// w ww .  ja v a2 s.co  m
 * 
 * @throws Exception
 */
public void testCWD() throws Exception {
    logger.debug("Start testCWD");

    FTPClient ftp = connectClient();

    try {
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            fail("FTP server refused connection.");
        }

        boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN);
        assertTrue("admin login successful", login);

        FTPFile[] files = ftp.listFiles();
        reply = ftp.getReplyCode();
        assertTrue(FTPReply.isPositiveCompletion(reply));

        assertTrue(files.length == 1);

        boolean foundAlfresco = false;
        for (FTPFile file : files) {
            logger.debug("file name=" + file.getName());
            assertTrue(file.isDirectory());

            if (file.getName().equalsIgnoreCase("Alfresco")) {
                foundAlfresco = true;
            }
        }
        assertTrue(foundAlfresco);

        // Change to Alfresco Dir that we know exists
        reply = ftp.cwd("/Alfresco");
        assertTrue(FTPReply.isPositiveCompletion(reply));

        // relative path with space char
        reply = ftp.cwd("Data Dictionary");
        assertTrue(FTPReply.isPositiveCompletion(reply));

        // non existant absolute
        reply = ftp.cwd("/Garbage");
        assertTrue(FTPReply.isNegativePermanent(reply));

        reply = ftp.cwd("/Alfresco/User Homes");
        assertTrue(FTPReply.isPositiveCompletion(reply));

        // Wild card
        reply = ftp.cwd("/Alfresco/User*Homes");
        assertTrue("unable to change to /Alfresco User*Homes/", FTPReply.isPositiveCompletion(reply));

        //            // Single char pattern match
        //            reply = ftp.cwd("/Alfre?co");
        //            assertTrue("Unable to match single char /Alfre?co", FTPReply.isPositiveCompletion(reply));

        // two level folder
        reply = ftp.cwd("/Alfresco/Data Dictionary");
        assertTrue("unable to change to /Alfresco/Data Dictionary", FTPReply.isPositiveCompletion(reply));

        // go up one
        reply = ftp.cwd("..");
        assertTrue("unable to change to ..", FTPReply.isPositiveCompletion(reply));

        reply = ftp.pwd();
        ftp.getStatus();

        assertTrue("unable to get status", FTPReply.isPositiveCompletion(reply));

        // check we are at the correct point in the tree
        reply = ftp.cwd("Data Dictionary");
        assertTrue(FTPReply.isPositiveCompletion(reply));

    } finally {
        ftp.disconnect();
    }

}

From source file:org.alinous.ftp.FtpManager.java

public String pwd(String sessionId) throws IOException {
    FTPClient ftp = this.instances.get(sessionId).getFtp();
    ftp.pwd();
    String cur = ftp.getReplyString();

    Matcher match = PWD_REPLY_MESSAGE.matcher(cur);
    if (!match.find() || match.groupCount() != 1) {
        return null;
    }//from w  w  w .  ja  v a 2 s . com
    return match.group(1);
}