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

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

Introduction

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

Prototype

public String getStatus() throws IOException 

Source Link

Document

Issue the FTP STAT command to the server.

Usage

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

/**
 * Test CWD for FTP server//from   w w  w  .j ava  2  s .com
 * 
 * @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:tufts.oki.remoteFiling.RemoteByteStore.java

/**
 *  Get the current length of thsi byte store.
 *
 *  @author Mark Norton//from  w  w  w .j av a2  s .c om
 *
 *  @return The current length of this byte store.
 */
public long length() throws osid.filing.FilingException {
    long length = 0;
    try {
        FTPClient client = rc.getClient();
        client.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
        //  The file to open consists of the root base plus, path to current directory plus name.
        //String fn = rc.getRootBase() + ((RemoteCabinet)getParent()).separator() + getDisplayName();
        //String fn = rc.getRootBase() + "/" + getDisplayName();
        String fn = getFullName();
        //System.out.println("length - file name to open: " + fn);
        FTPFile[] replies = client.listFiles(__fileListParser, fn);
        System.out.println("File Name = " + fn + " replies =" + replies + "Client:" + client.getStatus());
        if (replies == null) {
            System.out.println(client.getReplyCode());
            throw new osid.filing.FilingException(
                    "RemoteByteStore.length: " + osid.filing.FilingException.IO_ERROR);
        }
        //System.out.println(client.getReplyCode());
        length = replies[0].getSize();
    } catch (IOException e) {
        throw new osid.filing.FilingException(
                "RemoteByteStore.length: " + osid.filing.FilingException.IO_ERROR);
    }
    return length;
}

From source file:uk.sipperfly.utils.FTPUtil.java

/**
 * Upload whole directory (including its nested sub directories and files) to FTP server.
 *
 * @param ftpClient       an instance of org.apache.commons.net.ftp.FTPClient class.
 * @param remoteDirPath   Path of the destination directory on the server.
 * @param localParentDir  Path of the local directory being uploaded.
 * @param remoteParentDir Path of the parent directory of the current directory on the server (used by recursive calls).
 * @throws IOException if any network or IO error occurred.
 *//*from  w w w . j a  v  a 2  s  .c  o m*/
public static boolean uploadDirectory(FTPClient ftpClient, String remoteDirPath, String localParentDir,
        String remoteParentDir) throws IOException {

    System.out.println("LISTING directory: " + localParentDir);
    Logger.getLogger(GACOM).log(Level.INFO, "LISTING directory: {0}".concat(localParentDir));
    File localDir = new File(localParentDir);
    File[] subFiles = localDir.listFiles();
    if (subFiles != null && subFiles.length > 0) {
        for (File item : subFiles) {
            boolean answer = ftpClient.sendNoOp();
            if (!answer) {
                reconnect();
            }
            String status = ftpClient.getStatus();
            boolean a = ftpClient.isAvailable();
            //            if (!ftpClient.isConnected()) {
            //               reconnect();
            //            }
            String remoteFilePath = remoteDirPath + "/" + remoteParentDir + "/" + item.getName();
            if (remoteParentDir.equals("")) {
                remoteFilePath = remoteDirPath + "/" + item.getName();
            }
            if (item.isFile()) {
                // upload the file
                String localFilePath = item.getAbsolutePath();
                Logger.getLogger(GACOM).log(Level.INFO, "About to upload the file: ".concat(localFilePath));
                System.out.println("About to upload the file: " + localFilePath);
                boolean uploaded = uploadSingleFile(ftpClient, localFilePath, remoteFilePath);

                if (uploaded) {
                    Logger.getLogger(GACOM).log(Level.INFO, "UPLOADED a file to: ".concat(remoteFilePath));
                    System.out.println("UPLOADED a file to: " + remoteFilePath);
                } else {
                    System.out.println("COULD NOT upload the file: " + localFilePath);
                    Logger.getLogger(GACOM).log(Level.INFO,
                            "COULD NOT upload the file: ".concat(localFilePath));
                    Logger.getLogger(GACOM).log(Level.INFO, ftpClient.getReplyString());
                    return false;
                }
            } else {
                // create directory on the server
                boolean created = ftpClient.makeDirectory(remoteFilePath);
                if (created) {
                    System.out.println("CREATED the directory: " + remoteFilePath);
                    Logger.getLogger(GACOM).log(Level.INFO, "CREATED the directory: ".concat(remoteFilePath));
                } else {
                    System.out.println("COULD NOT create the directory: " + remoteFilePath);
                    Logger.getLogger(GACOM).log(Level.INFO,
                            "COULD NOT create the directory: ".concat(remoteFilePath));
                    Logger.getLogger(GACOM).log(Level.INFO, ftpClient.getReplyString());
                    return false;
                }

                // upload the sub directory
                String parent = remoteParentDir + "/" + item.getName();
                if (remoteParentDir.equals("")) {
                    parent = item.getName();
                }

                localParentDir = item.getAbsolutePath();
                uploadDirectory(ftpClient, remoteDirPath, localParentDir, parent);
            }
        }
    }
    return true;
}