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:ubicrypt.core.provider.ftp.FTProvider.java

private Observable<FTPClient> connect() {
    return Observable.<FTPClient>create(subscriber -> {
        final FTPClient client = new FTPClient();
        try {//from w w w .  j a  v  a  2  s .  co m
            client.connect(conf.getHost(), getConf().getPort() == -1 ? 21 : getConf().getPort());
            final int reply = client.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                log.error("FTP server refused connection:" + client.getReplyString());
                if (client.isConnected()) {
                    client.disconnect();
                }
                subscriber.onError(
                        new RuntimeException("FTP server refused connection:" + client.getReplyString()));
                return;
            }
            if (!getConf().isAnonymous()) {
                if (!client.login(getConf().getUsername(), new String(getConf().getPassword()))) {
                    client.disconnect();
                    log.warn("FTP wrong credentials:" + client.getReplyString());
                    subscriber.onError(new RuntimeException("FTP wrong credentials"));
                }
            }
            client.setFileType(FTP.BINARY_FILE_TYPE);
            client.setBufferSize(1 << 64);
            client.enterLocalPassiveMode();
            client.setControlKeepAliveTimeout(60 * 60); //1h
            if (!isEmpty(conf.getFolder())) {
                final String directory = startsWith("/", conf.getFolder()) ? conf.getFolder()
                        : "/" + conf.getFolder();
                if (!client.changeWorkingDirectory(directory)) {
                    if (!client.makeDirectory(directory)) {
                        disconnect(client);
                        subscriber.onError(new ProviderException(showServerReply(client)));
                        return;
                    }
                    if (!client.changeWorkingDirectory(directory)) {
                        disconnect(client);
                        subscriber.onError(new ProviderException(showServerReply(client)));
                        return;
                    }
                }
            }
            subscriber.onNext(client);
            subscriber.onCompleted();
        } catch (final IOException e) {
            disconnect(client);
            subscriber.onError(e);
        }
    }).subscribeOn(Schedulers.io());
}

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

public static boolean ftpPut(FTPClient ftp, String path, List<File> files, boolean autoLogout,
        boolean autoMkdir) throws IOException {
    boolean error = false;
    FileInputStream fis = null;/*ww w.  ja v a  2  s. c om*/

    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.");
                }
            }

            if (!ftp.changeWorkingDirectory(path)) {
                error = true;
                throw new IOException("Desired path does not exist on the server");
            }
        }

        log.info("All OK - transmitting " + files.size() + " file(s)");

        for (File f : files) {
            fis = new FileInputStream(f);
            if (!ftp.storeFile(f.getName(), fis)) {
                error = true;
                log.error("Error storing file: " + f.getName());
            }

            boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode());
            if (!success) {
                error = true;
                log.error("Error storing file: " + f.getName() + " (" + success + ")");
            }
        }

        if (autoLogout) {
            ftp.logout();
        }
    } catch (IOException e) {
        error = true;
        log.error("ftp", e);
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }

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

    // return inverse error boolean, just to make downstream conditionals easier
    return !error;
}

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;/*from  w  w w  .  ja  va  2  s.  c o  m*/

    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;

}

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.
 */// w w  w .j a v a  2s.  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;
}

From source file:websync2.SyncDesign.java

public void copyFileFTP(File temp, File servF) {

    InputStream inputStream = null;
    try {/*from  ww w  .  j a v a2s . c  o  m*/
        FTPClient ftpclient = ftp.getFtpClient();
        ftpclient.changeToParentDirectory();
        String firstRemoteFile = servF.getAbsolutePath();
        String[] pathA = firstRemoteFile.split("/");
        if (pathA != null) {
            for (int i = 0; i < pathA.length - 1; i++) {
                if ("".equals(pathA[i])) {
                    continue;
                }
                InputStream is = ftpclient.retrieveFileStream(pathA[i]);
                int retCode = ftpclient.getReplyCode();
                if (retCode == 550) {
                    ftpclient.makeDirectory(pathA[i]);
                }
                ftpclient.changeWorkingDirectory(pathA[i]);
            }
            inputStream = new FileInputStream(temp);
            boolean done = ftpclient.storeFile(pathA[pathA.length - 1], inputStream);
            if (done) {
                System.out.println("The first file is uploaded successfully.");
            }
        }

    } catch (FileNotFoundException ex) {
        Logger.getLogger(SyncDesign.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(SyncDesign.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            inputStream.close();
        } catch (IOException ex) {
            Logger.getLogger(SyncDesign.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}