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

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

Introduction

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

Prototype

public String printWorkingDirectory() throws IOException 

Source Link

Document

Returns the pathname of the current working directory.

Usage

From source file:org.structr.ftp.FtpDirectoriesTest.java

public void test04MkdirCdMkdirCd() {

    FTPClient ftp = setupFTPClient();

    try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {

        assertEmptyDirectory(ftp);//from  ww  w.  j  a  v a 2 s.  co m

        String name1 = "/FTPdir1";

        // Create folder by mkdir FTP command
        ftp.makeDirectory(name1);

        ftp.changeWorkingDirectory(name1);

        String newWorkingDirectory = ftp.printWorkingDirectory();
        assertEquals(name1, newWorkingDirectory);

        assertEmptyDirectory(ftp);

        String name2 = name1.concat("/").concat("FTPdir2");

        // Create folder by mkdir FTP command
        ftp.makeDirectory(name2);

        ftp.changeWorkingDirectory(name2);

        newWorkingDirectory = ftp.printWorkingDirectory();
        assertEquals(name2, newWorkingDirectory);

        assertEmptyDirectory(ftp);

        ftp.disconnect();

        tx.success();

    } catch (IOException | FrameworkException ex) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
}

From source file:uk.ac.bbsrc.tgac.miso.core.util.TransmissionUtils.java

public static boolean ftpPutListen(FTPClient ftp, String path, File file, boolean autoLogout, boolean autoMkdir,
        CopyStreamListener listener) throws IOException {
    boolean error = false;
    FileInputStream fis = null;//w w w.j ava 2  s. com

    log.info("ftpPutListen has been called for file:" + file.getName());
    try {
        if (ftp == null || !ftp.isConnected()) {
            error = true;
            throw new IOException(
                    "FTP client isn't connected. Please supply a client that has connected to the host.");
        }

        if (path != null) {
            if (autoMkdir) {
                if (!ftp.makeDirectory(path)) {
                    error = true;
                    throw new IOException("Cannot create desired path on the server.");
                }
            }
            log.info("Working dir =" + ftp.printWorkingDirectory());
            if (!ftp.changeWorkingDirectory(path)) {
                error = true;
                throw new IOException("Desired path does not exist on the server");
            }
        }

        fis = new FileInputStream(file);

        OutputStream ops = new BufferedOutputStream(ftp.storeFileStream(file.getName()), ftp.getBufferSize());

        log.info("TransmissionUtils putListen: FTP server responded: " + ftp.getReplyString());

        copyStream(fis, ops, ftp.getBufferSize(), file.length(), listener);

        ops.close();
        fis.close();
        log.info("TransmissionUtils putListen: FTP server responded: " + ftp.getReplyString());

        if (autoLogout) {
            ftp.logout();
        }
    } catch (IOException e) {
        error = true;
        log.error("ftp put listen", e);
    } finally {
        try {
            log.info("TransmissionUtils putListen:finally: " + ftp.getReplyString());
            if (fis != null) {
                fis.close();
            }

            if (autoLogout) {
                if (ftp != null && ftp.isConnected()) {
                    ftp.disconnect();
                }
            }
        } catch (IOException ioe) {
            log.error("ftp put listen close", ioe);
        }
    }

    // return inverse error boolean, just to make downstream conditionals easier
    log.info("result of transmissionutils.putListen:", !error);
    return !error;

}