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:FTPConnect.FTPSubirArchivo.java

public static void main(String[] args) throws IOException {
    FTPClient ftpclient = new FTPClient();
    FileInputStream fis = null;//ww  w . j  a va 2  s .  c  om
    boolean result;
    String ftpServerAddress = "localhost";
    String userName = "admin";
    String password = "admin";

    try {
        ftpclient.connect(ftpServerAddress);
        result = ftpclient.login(userName, password);

        if (result == true) {
            System.out.println("Logged in Successfully !");
        } else {
            System.out.println("Login Fail!");
            return;
        }
        ftpclient.setFileType(FTP.BINARY_FILE_TYPE);

        ftpclient.changeWorkingDirectory("/");

        File file = new File("D:/File.doc");
        String testName = file.getName();
        fis = new FileInputStream(file);

        // Upload file to the ftp server
        result = ftpclient.storeFile(testName, fis);

        if (result == true) {
            System.out.println("File is uploaded successfully");
        } else {
            System.out.println("File uploading failed");
        }
        ftpclient.logout();
    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
    } finally {
        try {
            ftpclient.disconnect();
        } catch (FTPConnectionClosedException e) {
            System.out.println(e);
        }
    }
}

From source file:ftp_server.FTPFileUpload.java

public static void main(String[] args) throws IOException {
    FTPClient ftpclient = new FTPClient();
    FileInputStream fis = null;/*from   ww  w .  jav a2s.  c  o  m*/
    boolean result;
    String ftpServerAddress = "shamalmahesh.net78.net";
    String userName = "a9959679";
    String password = "9P3IckDo";

    try {
        ftpclient.connect(ftpServerAddress);
        result = ftpclient.login(userName, password);

        if (result == true) {
            System.out.println("Logged in Successfully !");
        } else {
            System.out.println("Login Fail!");
            return;
        }
        ftpclient.enterLocalPassiveMode();
        ftpclient.setFileType(FTP.BINARY_FILE_TYPE);

        ftpclient.changeWorkingDirectory("/public_html/testing");

        File file = new File("D:/jpg/wq.jpg");
        String testName = file.getName();
        fis = new FileInputStream(file);

        // Upload file to the ftp server
        result = ftpclient.storeFile(testName, fis);

        if (result == true) {
            System.out.println("File is uploaded successfully");
        } else {
            System.out.println("File uploading failed");
        }
        ftpclient.logout();
    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
    } finally {
        try {
            ftpclient.disconnect();
        } catch (FTPConnectionClosedException e) {
            System.out.println(e);
        }
    }
}

From source file:luisjosediez.Ejercicio2.java

/**
 * @param args the command line arguments
 *//*from   ww w .  j  a v  a  2  s.c  o  m*/
public static void main(String[] args) {
    // TODO code application logic here
    System.out.println("Introduce la direccin de un servidor ftp: ");
    FTPClient cliente = new FTPClient();
    String servFTP = cadena();
    String clave = "";
    System.out.println("Introduce usuario (vaco para conexin annima): ");
    String usuario = cadena();
    String opcion;
    if (usuario.equals("")) {
        clave = "";
    } else {
        System.out.println("Introduce contrasea: ");
        clave = cadena();
    }
    try {
        cliente.setPassiveNatWorkaround(false);
        cliente.connect(servFTP, 21);
        boolean login = cliente.login(usuario, clave);
        if (login) {
            System.out.println("Conexin ok");
        } else {
            System.out.println("Login incorrecto");
            cliente.disconnect();
            System.exit(1);
        }
        do {

            System.out.println("Orden [exit para salir]: ");
            opcion = cadena();
            if (opcion.equals("ls")) {
                FTPFile[] files = cliente.listFiles();
                String tipos[] = { "Fichero", "Directorio", "Enlace" };
                for (int i = 0; i < files.length; i++) {
                    System.out.println("\t" + files[i].getName() + "\t=> " + tipos[files[i].getType()]);
                }
            } else if (opcion.startsWith("cd ")) {
                try {
                    cliente.changeWorkingDirectory(opcion.substring(3));
                } catch (IOException e) {
                }
            } else if (opcion.equals("help")) {
                System.out.println(
                        "Puede ejecutar los comandos 'exit', 'ls', 'cd', 'get' y 'upload'. Para ms detalles utilice 'help <comando>'.");
            } else if (opcion.startsWith("help ")) {
                if (opcion.endsWith(" get")) {
                    System.out.println(
                            "Permite descargar un archivo concreto. Uso: 'get <rutaArchivoADescargar>'.");
                } else if (opcion.endsWith(" ls")) {
                    System.out.println("Lista los ficheros y directorios en la ubicacin actual. Uso: 'ls'.");
                } else if (opcion.endsWith(" cd")) {
                    System.out.println("Permite cambiar la ubicacin actual. Uso: 'cd <rutaDestino>'.");
                } else if (opcion.endsWith(" put")) {
                    System.out.println(
                            "Permite subir un archivo al directorio actual. Uso: 'put <rutaArchivoASubir>'.");
                }
            } else if (opcion.startsWith("get ")) {
                try {
                    System.out.println("Indique la carpeta de descarga: ");
                    try (FileOutputStream fos = new FileOutputStream(cadena() + opcion.substring(4))) {
                        cliente.retrieveFile(opcion.substring(4), fos);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                }
            } else if (opcion.startsWith("put ")) {
                try {
                    try {

                        System.out.println(opcion.substring(4));

                        File local = new File(opcion.substring(4));

                        System.out.println(local.getName());

                        InputStream is = new FileInputStream(opcion.substring(4));

                        OutputStream os = cliente.storeFileStream(local.getName());

                        byte[] bytesIn = new byte[4096];

                        int read = 0;

                        while ((read = is.read(bytesIn)) != -1) {

                            os.write(bytesIn, 0, read);

                        }

                        is.close();
                        os.close();

                        boolean completed = cliente.completePendingCommand();

                        if (completed) {
                            System.out.println("The file is uploaded successfully.");
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                }
            }

        } while (!(opcion.equals("exit")));

        boolean logout = cliente.logout();
        if (logout)
            System.out.println("Logout...");
        else
            System.out.println("Logout incorrecto");

        cliente.disconnect();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.axelor.apps.tool.net.MyFtp.java

public static void getDataFiles(String server, String username, String password, String folder,
        String destinationFolder, Calendar start, Calendar end) {

    try {/*ww  w .  j a  v a  2  s. c  om*/

        // Connect and logon to FTP Server
        FTPClient ftp = new FTPClient();
        ftp.connect(server);
        ftp.login(username, password);

        // List the files in the directory
        ftp.changeWorkingDirectory(folder);
        FTPFile[] files = ftp.listFiles();

        for (int i = 0; i < files.length; i++) {

            Date fileDate = files[i].getTimestamp().getTime();
            if (fileDate.compareTo(start.getTime()) >= 0 && fileDate.compareTo(end.getTime()) <= 0) {

                // Download a file from the FTP Server
                File file = new File(destinationFolder + File.separator + files[i].getName());
                FileOutputStream fos = new FileOutputStream(file);
                ftp.retrieveFile(files[i].getName(), fos);
                fos.close();
                file.setLastModified(fileDate.getTime());
            }
        }

        // Logout from the FTP Server and disconnect
        ftp.logout();
        ftp.disconnect();

    } catch (Exception e) {

        LOG.error(e.getMessage());

    }
}

From source file:atualizador.Atualizador.java

private static void FTPDownload(String fileName, String DiretorioFTP)
        throws IOException, FileNotFoundException {
    FTPClient client = new FTPClient();
    FileOutputStream fos = null;/*from  w w w.j  a v  a 2s. c o m*/

    client.connect("plutao.telemar");
    client.login("usrcorj", "usr341!tg");

    String filename = fileName;
    fos = new FileOutputStream(filename);

    client.changeWorkingDirectory(DiretorioFTP);
    client.retrieveFile(filename, fos);
    fos.close();
    client.disconnect();
}

From source file:ftp.FTPtask.java

/**
 * ? ? ? ??      Folder/*  w  w w  .jav a 2  s  .  c  o m*/
 * @param FTPADDR, ?  ?
 * @param user,   
 * @param password,   
 * @param Folder,    ?? ?
 * @return String[] result, ?? /
 */

public static String[] GetList(String FTPADDR, String user, String password, String Folder) {
    String[] result;
    result = null;

    FTPClient client = new FTPClient();

    try {
        client.connect(FTPADDR);
        client.login(user, password);
        client.changeWorkingDirectory(Folder);
        result = client.listNames();
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:com.savy3.util.MainframeFTPClientUtils.java

public static List<String> listSequentialDatasets(String pdsName, Configuration conf) throws IOException {
    List<String> datasets = new ArrayList<String>();
    FTPClient ftp = null;
    try {/*from  w  w w.jav  a2s. c  om*/
        ftp = getFTPConnection(conf);
        if (ftp != null) {
            ftp.changeWorkingDirectory("'" + pdsName + "'");
            FTPFile[] ftpFiles = ftp.listFiles();
            for (FTPFile f : ftpFiles) {
                if (f.getType() == FTPFile.FILE_TYPE) {
                    datasets.add(f.getName());
                }
            }
        }
    } catch (IOException ioe) {
        throw new IOException("Could not list datasets from " + pdsName + ":" + ioe.toString());
    } finally {
        if (ftp != null) {
            closeFTPConnection(ftp);
        }
    }
    return datasets;
}

From source file:CFDI_Verification.CFDI_Verification.java

public static String test_Connection(String directory) {

    String server = "192.1.1.64";
    String user = "ftpuser";
    String pass = "Oracle123";
    String result = "No Connected!";

    FTPClient ftpClient = new FTPClient();

    try {/*from  w w  w. jav  a  2  s .c  om*/
        ftpClient.connect(server);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();
        ftpClient.changeWorkingDirectory(directory);

        if (ftpClient.isConnected() == true) {
            result = "Connected to " + ftpClient.printWorkingDirectory();
        }

        ftpClient.logout();
        ftpClient.disconnect();

    } catch (IOException ex) {
        System.out.println("Ooops! Error en conexin." + ex.getMessage());
    } finally {
        return result;
    }
}

From source file:CFDI_Verification.CFDI_Verification.java

public static void list_directories(String directory) {
    String server = "192.1.1.64";
    String user = "ftpuser";
    String pass = "Oracle123";
    String result = "No Connected!";

    FTPClient ftpClient = new FTPClient();

    try {/* w w  w  . j  av  a 2  s .c  om*/
        ftpClient.connect(server);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();
        ftpClient.changeWorkingDirectory(directory);

        String[] directories = ftpClient.listNames();
        String dir;

        for (int i = 0; i < directories.length; i++) {
            System.out.println(directories[i]);
            dir = directories[i];
        }

        ftpClient.logout();
        ftpClient.disconnect();

    } catch (IOException ex) {
        System.out.println("Ooops! Error en conexin." + ex.getMessage());
    }
}

From source file:CFDI_Verification.CFDI_Verification.java

public static ARRAY getDirectories(String directory) throws SQLException {
    Connection conn = new OracleDriver().defaultConnection();
    ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("DIRECTORIES_LIST", conn);

    String server = "192.1.1.64";
    String user = "ftpuser";
    String pass = "Oracle123";
    String result = "No Connected!";

    FTPClient ftpClient = new FTPClient();

    try {//from  w ww.  ja  v  a2s  . c  om
        ftpClient.connect(server);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();
        ftpClient.changeWorkingDirectory(directory);

        String[] directories = ftpClient.listNames();
        ARRAY directories_list = new ARRAY(descriptor, conn, directories);

        ftpClient.logout();
        ftpClient.disconnect();

        return directories_list;
    } catch (IOException ex) {
        System.out.println("Ooops! Error en conexin." + ex.getMessage());
    }
    return null;
}