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

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

Introduction

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

Prototype

public boolean makeDirectory(String pathname) throws IOException 

Source Link

Document

Creates a new subdirectory on the FTP server in the current directory (if a relative pathname is given) or where specified (if an absolute pathname is given).

Usage

From source file:ro.kuberam.libs.java.ftclient.FTP.FTP.java

public boolean storeResource(Object abstractConnection, String remoteDirectoryPath, String resourceName,
        InputStream resourceInputStream) throws Exception {
    long startTime = new Date().getTime();
    FTPClient connection = (FTPClient) abstractConnection;
    if (!connection.isConnected()) {
        throw new Exception(ErrorMessages.err_FTC002);
    }/* www .  j ava  2  s  .  c om*/

    Boolean result = true;
    try {
        if (resourceName.length() == 0) {
            resourceName = remoteDirectoryPath.substring(remoteDirectoryPath.lastIndexOf("/") + 1);
            remoteDirectoryPath = remoteDirectoryPath.substring(0, remoteDirectoryPath.lastIndexOf("/"));
            _checkResourcePath(connection, remoteDirectoryPath, "store-resource", true);
            result = connection.makeDirectory(resourceName);
        } else {
            _checkResourcePath(connection, remoteDirectoryPath, "store-resource", false);
            result = connection.storeFile(resourceName, resourceInputStream);
            resourceInputStream.close();
        }
    } catch (IOException ioe) {
        log.error(ioe.getMessage(), ioe);
        // TODO: add throw exception here for cases when server doesn't
        // allow storage of file - a use case is when vsftpd was configured
        // with mandatory SSL encryption
        result = false;
    }

    // if(!FTPconnection.completePendingCommand()) {
    // throw new Exception(
    // "err:FTC007: The current operation failed.");
    // }

    log.info("The FTP sub-module stored the resource '" + resourceName + "' at '" + remoteDirectoryPath
            + "' in " + (new Date().getTime() - startTime) + " ms.");

    return result;
}

From source file:ru.in360.FTPUploader.java

public boolean upload(File src, URI dest) {
    FTPClient ftpClient = new FTPClient();

    try {//from  w w  w .  j a v  a2s  .c  o  m
        // connect and login to the server
        ftpClient.connect(server, port);
        ftpClient.login(user, pass);

        // use local passive mode to pass firewall
        ftpClient.enterLocalPassiveMode();
        //ftpClient.makeDirectory(remoteProject);

        String path = "";
        for (String dir : (remoteProject + dest.getPath()).split("/")) {
            ftpClient.makeDirectory(path + "/" + dir);
            path = path + "/" + dir;
        }

        System.out.println("Connected");

        System.out.println(remoteProject + dest.getPath());
        FTPUtil.saveFilesToServer(ftpClient, remoteProject + dest.getPath(), src);
        // log out and disconnect from the server
        ftpClient.logout();
        ftpClient.disconnect();

        System.out.println("Disconnected");
        return true;
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    }
}

From source file:ru.in360.FTPUtil.java

public static void upload(File src, FTPClient ftp) throws IOException {
    if (src.isDirectory()) {
        ftp.makeDirectory(src.getName());
        ftp.changeWorkingDirectory(src.getName());
        for (File file : src.listFiles()) {
            upload(file, ftp);//from  w  w  w .jav a  2s.c o  m
        }
        ftp.changeToParentDirectory();
    } else {
        InputStream srcStream = null;
        try {
            srcStream = src.toURI().toURL().openStream();
            ftp.storeFile(src.getName(), srcStream);
        } finally {
            IOUtils.closeQuietly(srcStream);
        }
    }
}

From source file:s32a.CodebaseDeployer.java

/**
 * Uploads given files to ftp server./*ww w. j  a  v  a 2 s .c o m*/
 *
 * @param input key: desired name on server, Value: file to upload.
 */
private void uploadFiles(Map<String, File> input) {

    FTPClient client = null;
    if (SSL) {
        client = new FTPSClient(false);
    } else {
        client = new FTPClient();
    }
    FileInputStream fis = null;
    FileOutputStream fos = null;

    try {
        System.out.println("connecting");
        client.connect(ftpServer);
        boolean login = client.login(this.userName, this.password);
        System.out.println("login: " + login);
        client.enterLocalPassiveMode();

        //            client.setFileType(FTP.ASCII_FILE_TYPE);

        //Creates all directories required on the server
        System.out.println("creating directories");
        client.makeDirectory("Airhockey");
        client.makeDirectory("Airhockey/Codebase");
        client.makeDirectory("Airhockey/Servers");
        client.makeDirectory("Airhockey/Codebase/s32a");
        System.out.println("default directories made");
        for (String s : directories) {
            client.makeDirectory(s);
        }

        //Uploads codebase URL
        fis = new FileInputStream(this.codebaseFile);
        boolean stored = client.storeFile("Airhockey/Codebase/codebase.properties", fis);
        //            client.completePendingCommand();
        System.out.println("Stored codebase file: " + stored);
        fis.close();

        // Removes references to all servers
        for (FTPFile f : client.listFiles("Airhockey/Servers")) {
            if (f.isFile()) {
                System.out.println("Deleting Server Listing: " + f.getName());
                client.deleteFile("/Airhockey/Servers/" + f.getName());
            }
        }

        // Uploads all class files
        System.out.println("Uploading classes");
        String defaultLoc = fs + "Airhockey" + fs + "Codebase" + fs;
        for (String dest : input.keySet()) {
            fis = new FileInputStream(input.get(dest));
            if (!client.storeFile(defaultLoc + dest, fis)) {
                System.out.println("unable to save: " + defaultLoc + dest);
            }
            fis.close();
            //                client.completePendingCommand();
        }

        client.logout();
    } catch (IOException ex) {
        System.out.println("IOException: " + ex.getMessage());
        ex.printStackTrace();
    } catch (Exception ex) {
        System.out.println("exception caught: " + ex.getMessage());
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:s32a.FTPTest.java

public FTPTest(boolean login) {

    FTPClient client = new FTPClient();
    FileInputStream fis = null;//from   w  w  w  .  ja v  a 2 s .c o  m
    FileOutputStream fos = null;

    try {
        client.connect("s32a.Airhockey.org");
        client.login("testey", "test");
        client.enterLocalPassiveMode();
        client.setFileType(FTP.ASCII_FILE_TYPE);

        client.makeDirectory("/testey");

        //        String filename = "testey.txt";
        //        File file = new File(filename);
        //        file.createNewFile();
    } catch (IOException ex) {
        Logger.getLogger(FTPTest.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:s32a.FTPTest.java

public FTPTest() {
    FTPClient client = new FTPSClient(false);
    FileInputStream fis = null;//from   ww w.  java  2 s .  c  o  m
    FileOutputStream fos = null;

    try {
        System.out.println("connecting");
        client.connect("athena.fhict.nl");
        boolean login = client.login("i293443", "ifvr2edfh101");
        System.out.println("login: " + login);
        client.enterLocalPassiveMode();
        System.out.println("connected: " + client.isConnected() + ", available: " + client.isAvailable());

        client.setFileType(FTP.ASCII_FILE_TYPE);
        //
        // Create an InputStream of the file to be uploaded
        //
        String filename = ".gitattributes";
        File file = new File(filename);
        file.createNewFile();
        System.out.println(file.length());
        fis = new FileInputStream(file.getAbsolutePath());
        client.makeDirectory("/Airhockey/Codebase/test");
        client.makeDirectory("\\Airhockey\\Codebase\\testey");

        //
        // Store file to server
        //
        String desiredName = "s32a\\Server\\.gitattributes";
        System.out.println("storefile: " + file.getAbsolutePath() + " - "
                + client.storeFile("/Airhockey/" + desiredName, fis));
        System.out.println("file stored");

        //            File output = new File("colors.json");
        //            fos = new FileOutputStream(output.getAbsolutePath());
        //            client.retrieveFile("/colors.json", fos);
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception ex) {
        System.out.println("exception caught: " + ex.getMessage());
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:se.natusoft.maven.plugin.ftp.FTPMojo.java

/**
 * Create a full path directory by creating one part at a time.
 *
 * @param ftpClient A connected FTPClient instance.
 * @param dir The directory to create.//  w w  w.j  a v a  2  s.c om
 *
 * @return true on success.
 *
 * @throws IOException on failure.
 */
private boolean mkdir(FTPClient ftpClient, String dir) throws IOException {
    boolean result = true;

    for (String part : dir.split("/")) {
        if (!ftpClient.changeWorkingDirectory(part)) {
            result = ftpClient.makeDirectory(part);
            if (!result)
                return result;
            result = ftpClient.changeWorkingDirectory(part);
            if (!result)
                return result;
        }
    }

    return result;
}

From source file:se.vgregion.webbisar.helpers.FileHandler.java

public void writeTempFile(String fileName, String sessionId, InputStream is) throws FTPException {
    FTPClient ftp = connect();
    try {// www.  j  ava 2s  .co m
        ftp.makeDirectory("temp");
        ftp.changeWorkingDirectory("temp");
        ftp.makeDirectory(sessionId);
        ftp.changeWorkingDirectory(sessionId);
        ftp.storeFile(fileName, is);
        ftp.logout();
    } catch (IOException e) {
        LOGGER.error("Could not write tempfile " + fileName, e);
    } finally {
        try {
            ftp.disconnect();
        } catch (IOException e) {
            // Empty...
        }
    }

}

From source file:simplehttpdb.net.FTPHelper.java

private void createDirIfNotExists(FTPClient ftpClient, String path) throws IOException {

    //TODO: find a better way to check whether directory exists

    //        Logger.getLogger(getClass().getName()).log(Level.INFO,
    //                "trying to create dir:" + path);

    if (ftpClient.makeDirectory(path)) {
        //            Logger.getLogger(getClass().getName()).log(Level.INFO,
        //                    "done. "+ftpClient.getReplyString());
    } else {//from  w w  w  .ja v a 2  s  .c om
        //            Logger.getLogger(getClass().getName()).log(Level.INFO,
        //                    "failed. "+ftpClient.getReplyString());
    }

}

From source file:tufts.oki.remoteFiling.RemoteCabinet.java

/**
 *  Create a new cabinet entry with/*from  w  w w . j  a v a 2 s. co  m*/
 *  the agentOwner of this new cabinet as this owner of this Cabinet.
 *
 *  @author Mark Norton
 *
 *  @return A new cabinet with the given displayName.
 */
public osid.filing.Cabinet createCabinet(String displayName) throws osid.filing.FilingException {

    //  Create the new cabinet entry.
    osid.shared.Agent agentOwner = super.getCabinetEntryAgent();
    RemoteCabinet entry = new RemoteCabinet(displayName, agentOwner, this, rc);

    //  Make a directory on the remote file system.
    try {
        FTPClient client = rc.getClient();
        if (!client.makeDirectory(entry.getFullName()))
            throw new osid.filing.FilingException(osid.filing.FilingException.ITEM_ALREADY_EXISTS);
    } catch (java.io.IOException ex) {
        throw new osid.filing.FilingException(osid.filing.FilingException.IO_ERROR);
    }

    /*  Add the element to the Vector array.  */
    children.addElement(entry);

    return entry;
}