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

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

Introduction

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

Prototype

public boolean changeWorkingDirectory(String pathname) throws IOException 

Source Link

Document

Change the current working directory of the FTP session.

Usage

From source file:com.qasp.diego.arsp.Atualiza.java

public static boolean DownloadFTP() throws IOException {

    final String FTPURL = "37.187.45.24";
    final String USUARIO = "diego";
    final String SENHA = "Jogador5";
    final String ARQUIVO = "data.dat";
    FTPClient ftp = new FTPClient();

    try {//ww  w . ja  v a2  s. c  om
        ftp.connect(FTPURL);
        ftp.login(USUARIO, SENHA);
        ftp.changeWorkingDirectory("/httpdocs/tcc/TCCpython");
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        OutputStream outputStream = null;
        boolean downloadcomsucesso = false;
        try {
            File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                    ARQUIVO);
            f.createNewFile();
            outputStream = new BufferedOutputStream(new FileOutputStream(f));
            downloadcomsucesso = ftp.retrieveFile(ARQUIVO, outputStream);
        } finally {
            if (outputStream != null)
                outputStream.close();
        }
        return downloadcomsucesso;
    } finally {
        if (ftp != null) {
            ftp.logout();
            ftp.disconnect();
        }
    }
}

From source file:de.jwi.ftp.FTPUploader.java

private static String uploadFiles(FTPClient ftp, String ftpServerPath, List files) throws IOException {
    boolean rc = false;
    String rcs = "error";
    List l = null;/*from  w w  w  . j a va  2  s.c  o  m*/

    rc = ftp.makeDirectory(ftpServerPath);
    boolean rc1 = ftp.changeWorkingDirectory(ftpServerPath);
    System.out.println("cd: " + ftpServerPath);

    if (rc1) {
        Iterator it = files.iterator();
        while (it.hasNext()) {
            File f = (File) it.next();

            if (f.isDirectory()) {
                String name = f.getName();

                String newPath = ftpServerPath + "/" + name;

                l = Arrays.asList(f.listFiles());

                rcs = uploadFiles(ftp, newPath, l);

                rc1 = ftp.changeWorkingDirectory(ftpServerPath);

                if (!rc1) {
                    return "failed to chdir to " + ftpServerPath;
                }
            } else {
                rcs = uploadFile(ftp, ftpServerPath, f);
            }
        }
    } else {
        rcs = "failed to chdir to " + ftpServerPath;
    }
    return rcs;
}

From source file:deincraftlauncher.IO.download.FTPSync.java

public static String[] listFiles(String dir) {

    FTPClient client = new FTPClient();

    try {/*  w  ww.j a  v a2  s . c o m*/
        client.connect(ftpServer);
        client.login(ftpUsername, ftpPassword);
        client.changeWorkingDirectory(dir);

        // Obtain a list of filenames in the current working
        // directory. When no file found an empty array will 
        // be returned.
        String[] names = client.listNames();

        client.logout();

        return names;

    } catch (IOException e) {
        System.err.println("Error connecting to ftp (listfiles): " + e);
    } finally {

        try {
            client.disconnect();
        } catch (IOException e) {
            System.err.println("Error disconnecting to ftp (listfiles): " + e);
        }
    }

    return null;

}

From source file:com.clothcat.hpoolauto.HtmlUploader.java

/**
 * Upload the html via FTP//from  w w w  .ja  v  a  2  s  . c  o m
 */
public static void uploadHtml() {
    FTPClient ftpc = new FTPClient();
    FTPClientConfig conf = new FTPClientConfig();
    ftpc.configure(conf);
    try {
        ftpc.connect(Constants.FTP_HOSTNAME);
        ftpc.login(Constants.FTP_USERNAME, Constants.FTP_PASSWORD);
        File dir = new File(Constants.HTML_FILEPATH);
        File[] files = dir.listFiles();
        ftpc.changeWorkingDirectory("/htdocs");
        for (File f : files) {
            HLogger.log(Level.FINEST, "Uploading file: " + f.getName());
            FileInputStream fis = new FileInputStream(f);
            ftpc.storeFile(f.getName(), fis);
        }
        ftpc.logout();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        if (ftpc.isConnected()) {
            try {
                ftpc.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:com.claim.support.FtpUtil.java

public static void ftpCreateDirectoryTree(FTPClient client, String dirTree) throws IOException {

    boolean dirExists = true;

    //tokenize the string and attempt to change into each directory level.  If you cannot, then start creating.
    String[] directories = dirTree.split("/");
    for (String dir : directories) {
        if (!dir.isEmpty()) {
            if (dirExists) {
                dirExists = client.changeWorkingDirectory(dir);
            }/* w  w w  . j a va  2 s .  co  m*/
            if (!dirExists) {
                if (!client.makeDirectory(dir)) {
                    throw new IOException("Unable to create remote directory '" + dir + "'.  error='"
                            + client.getReplyString() + "'");
                }
                if (!client.changeWorkingDirectory(dir)) {
                    throw new IOException("Unable to change into newly created remote directory '" + dir
                            + "'.  error='" + client.getReplyString() + "'");
                }
            }
        }
    }
}

From source file:com.geotrackin.gpslogger.senders.ftp.Ftp.java

private static void ftpCreateDirectoryTree(FTPClient client, String dirTree) throws IOException {

    boolean dirExists = true;

    //tokenize the string and attempt to change into each directory level.  If you cannot, then start creating.
    String[] directories = dirTree.split("/");
    for (String dir : directories) {
        if (dir.length() > 0) {
            if (dirExists) {
                dirExists = client.changeWorkingDirectory(dir);
                showServerReply(client);
            }/*from   w  ww .j av  a2  s.  c o  m*/
            if (!dirExists) {
                client.makeDirectory(dir);
                showServerReply(client);
                client.changeWorkingDirectory(dir);
                showServerReply(client);
            }
        }
    }
}

From source file:com.microsoft.azure.management.appservice.samples.ManageWebAppSourceControl.java

private static void uploadFileToFtp(PublishingProfile profile, String fileName, InputStream file)
        throws Exception {
    FTPClient ftpClient = new FTPClient();
    String[] ftpUrlSegments = profile.ftpUrl().split("/", 2);
    String server = ftpUrlSegments[0];
    String path = "./site/wwwroot/webapps";
    ftpClient.connect(server);//from  w w  w .j  ava  2s . c  o m
    ftpClient.login(profile.ftpUsername(), profile.ftpPassword());
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    ftpClient.changeWorkingDirectory(path);
    ftpClient.storeFile(fileName, file);
    ftpClient.disconnect();
}

From source file:com.jaspersoft.jasperserver.api.engine.common.util.impl.FTPUtil.java

private static void changeDirectory(FTPClient ftpClient, String directoryPath) throws Exception {
    if (ftpClient == null)
        throw new JSException("Please connect to FTP server first before changing directory!");
    if (log.isDebugEnabled())
        log.debug("Original Working directory = " + ftpClient.printWorkingDirectory());
    ftpClient.changeWorkingDirectory(directoryPath);
    if (log.isDebugEnabled())
        log.debug("NEW Working directory = " + ftpClient.printWorkingDirectory());
}

From source file:cycronix.ctlib.CTftp.java

/**
* utility to create an arbitrary directory hierarchy on the remote ftp server 
* @param client//from  w  w w .j  a  v  a 2 s.  co  m
* @param dirTree  the directory tree only delimited with / chars.  No file name!
* @throws Exception
*/
private static void ftpCreateDirectoryTree(FTPClient client, String dirTree) throws IOException {
    boolean dirExists = true;
    //tokenize the string and attempt to change into each directory level.  If you cannot, then start creating.
    String[] directories = dirTree.split("/");
    for (String dir : directories) {

        if (!dir.isEmpty()) {
            if (dirExists) {
                dirExists = client.changeWorkingDirectory(dir);
            }

            if (!dirExists) {
                try {
                    if (!client.makeDirectory(dir)) {
                        throw new IOException("Unable to create remote directory: " + dir + ", error="
                                + client.getReplyString());
                    }
                    if (!client.changeWorkingDirectory(dir)) {
                        throw new IOException("Unable to change into newly created remote directory: " + dir
                                + ", error=" + client.getReplyString());
                    }
                } catch (IOException ioe) {
                    //                  System.err.println("ftpCreateDir exception on dirTree: "+dirTree+", dir: "+dir+", error: "+ioe.getMessage());
                    throw ioe;
                }
            }
        }
    }
}

From source file:com.ephesoft.dcma.util.FTPUtil.java

/**
 * API to download files from an arbitrary directory hierarchy on the remote ftp server
 * //from w w w . j a v  a2s  .c o m
 * @param client {@link FTPClient}
 * @param ftpDirectory{{@link String} the directory tree only delimited with / chars. No file name!
 * @param destDirectoryPath {@link String} the path where file will be downloaded.
 * @throws Exception
 */
public static void retrieveFiles(final FTPClient client, final String ftpDirectory,
        final String destDirectoryPath) throws IOException {
    boolean dirExists = true;
    String[] directories = ftpDirectory.split(DIRECTORY_SEPARATOR);
    for (String dir : directories) {
        if (!dir.isEmpty()) {
            if (dirExists) {
                dirExists = client.changeWorkingDirectory(dir);
            }
        }
    }
    FTPFile[] fileList = client.listFiles();
    if (fileList != null && fileList.length > 0) {
        String outputFilePath;
        FileOutputStream fileOutputStream;
        for (FTPFile file : fileList) {
            outputFilePath = EphesoftStringUtil.concatenate(destDirectoryPath, File.separator, file.getName());
            fileOutputStream = new FileOutputStream(outputFilePath);
            client.retrieveFile(file.getName(), fileOutputStream);
            fileOutputStream.close();
        }
    }
}