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

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

Introduction

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

Prototype

public FTPClient() 

Source Link

Document

Default FTPClient constructor.

Usage

From source file:core.connectToFTPServer.java

public connectToFTPServer(String ip, int port, String user, String password) throws IOException {

    ftpClient = new FTPClient();
    ftpClient.connect(ip, port);//from  w  w w  .ja  va 2  s  .c o m
    ftpClient.login(user, password);
    ftpClient.enterLocalPassiveMode();
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    listParseEnfine = ftpClient.initiateListParsing();
    System.out.println("connected to FTP server");
}

From source file:br.com.travelmate.util.Ftp.java

public Ftp(String host, String user, String password) {
    ftpClient = new FTPClient();

    //this.host = "tmftp.systm.com.br";
    this.host = host;
    this.user = user;
    this.password = password;
}

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

public synchronized static boolean Upload(String server, String username, String password, String directory,
        int port, boolean useFtps, String protocol, boolean implicit, InputStream inputStream,
        String fileName) {/*w w  w. jav a  2 s . co  m*/
    FTPClient client = null;

    try {
        if (useFtps) {
            client = new FTPSClient(protocol, implicit);

            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(null, null);
            KeyManager km = kmf.getKeyManagers()[0];
            ((FTPSClient) client).setKeyManager(km);
        } else {
            client = new FTPClient();
        }

    } catch (Exception e) {
        tracer.error("Could not create FTP Client", e);
        return false;
    }

    try {
        tracer.debug("Connecting to FTP");
        client.connect(server, port);
        showServerReply(client);

        tracer.debug("Logging in to FTP server");
        if (client.login(username, password)) {
            client.enterLocalPassiveMode();
            showServerReply(client);

            tracer.debug("Uploading file to FTP server " + server);

            tracer.debug("Checking for FTP directory " + directory);
            FTPFile[] existingDirectory = client.listFiles(directory);
            showServerReply(client);

            if (existingDirectory.length <= 0) {
                tracer.debug("Attempting to create FTP directory " + directory);
                //client.makeDirectory(directory);
                ftpCreateDirectoryTree(client, directory);
                showServerReply(client);

            }

            client.changeWorkingDirectory(directory);
            boolean result = client.storeFile(fileName, inputStream);
            inputStream.close();
            showServerReply(client);
            if (result) {
                tracer.debug("Successfully FTPd file " + fileName);
            } else {
                tracer.debug("Failed to FTP file " + fileName);
                return false;
            }

        } else {
            tracer.debug("Could not log in to FTP server");
            return false;
        }

    } catch (Exception e) {
        tracer.error("Could not connect or upload to FTP server.", e);
        return false;
    } finally {
        try {
            tracer.debug("Logging out of FTP server");
            client.logout();
            showServerReply(client);

            tracer.debug("Disconnecting from FTP server");
            client.disconnect();
            showServerReply(client);
        } catch (Exception e) {
            tracer.error("Could not logout or disconnect", e);
            return false;
        }
    }

    return true;
}

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

public static String upload(URL url, List files) {
    String rcs = "";

    if (!"ftp".equals(url.getProtocol())) {
        return "not ftp protocol";
    }// w  w  w .  j  av  a2 s.co  m

    String host = url.getHost();
    String userInfo = url.getUserInfo();
    String path = url.getPath();
    String user = null;
    String pass = null;

    int p;
    if ((userInfo != null) && ((p = userInfo.indexOf(':')) > -1)) {
        user = userInfo.substring(0, p);
        pass = userInfo.substring(p + 1);
    } else {
        user = userInfo;
    }

    FTPClient ftp = new FTPClient();

    try {
        int reply;
        ftp.connect(host);

        // After connection attempt, you should check the reply code to verify
        // success.
        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return "connection refused";
        }
    } catch (IOException e) {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
        return "could not connect to " + host;
    }

    try {
        if (!ftp.login(user, pass)) {
            ftp.logout();
            return "failed to login";
        }

        ftp.setFileType(FTP.BINARY_FILE_TYPE);

        // Use passive mode as default because most of us are
        // behind firewalls these days.
        ftp.enterLocalPassiveMode();

        rcs = uploadFiles(ftp, path, files);

        ftp.logout();
    } catch (FTPConnectionClosedException e) {
        return "connection closed";
    } catch (IOException e) {
        return e.getMessage();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
    }

    return rcs;
}

From source file:de.l3s.dlg.ncbikraken.ftp.NcbiFTPClient.java

public static void getMedline(MedlineFileType type) {
    FileOutputStream out = null;/*from  w ww .ja  v a  2s . c  o  m*/
    FTPClient ftp = new FTPClient();
    try {
        // Connection String
        LOGGER.info("Connecting to FTP server " + SERVER_NAME);
        ftp.connect(SERVER_NAME);
        ftp.login("anonymous", "");
        ftp.cwd(BASELINE_PATH);
        ftp.cwd(type.getServerPath());

        try {
            ftp.pasv();
        } catch (IOException e) {
            LOGGER.error(
                    "Can not access the passive mode. Maybe a problem with your (Windows) firewall. Just try to run as administrator: \nnetsh advfirewall set global StatefulFTP disable");
            return;
        }

        for (FTPFile file : ftp.listFiles()) {
            if (file.isFile()) {
                File meshF = new File(file.getName());
                LOGGER.debug("Downloading file: " + SERVER_NAME + ":" + BASELINE_PATH + "/"
                        + type.getServerPath() + "/" + meshF.getName());
                out = new FileOutputStream(Configuration.INSTANCE.getMedlineDir() + File.separator + meshF);
                ftp.retrieveFile(meshF.getName(), out);
                out.flush();
                out.close();
            }
        }

    } catch (IOException ioe) {
        LOGGER.error(ioe.getMessage());

    } finally {
        IOUtils.closeQuietly(out);
        try {
            ftp.disconnect();
        } catch (IOException e) {
            LOGGER.error(e.getMessage());
        }
    }

}

From source file:de.idos.updates.install.FtpFileInstaller.java

private void connect(InetAddress inetAddress) {
    try {//from   w  ww.  j av  a 2s  .c o m
        ftpClient = new FTPClient();
        ftpClient.connect(inetAddress);
        ftpClient.enterLocalPassiveMode();

        if (login != null) {
            ftpClient.login(login, null);
        }

        if (workingDir != null) {
            ftpClient.changeWorkingDirectory(workingDir);
        }

    } catch (IOException ex) {
        report.versionLookupFailed(ex);
        ftpClient = null;
    }
}

From source file:com.griffinslogistics.FTP.DeliveryFTP.java

public DeliveryFTP() {
    this.ftpClient = new FTPClient();
}

From source file:model.ProfilModel.java

public void uploadFoto(String path) {
    String server = "localhost";
    int port = 21;
    String user = "user1";
    String pass = "itsme";

    FTPClient ftpClient = new FTPClient();
    try {//from   ww  w . ja va2 s. c  o  m

        ftpClient.connect(server, port);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();

        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        // APPROACH #1: uploads first file using an InputStream
        File firstLocalFile = new File(path);

        String firstRemoteFile = this.user.getFoto();
        InputStream inputStream = new FileInputStream(firstLocalFile);

        System.out.println("Start uploading first file");
        boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
        inputStream.close();
        if (done) {
            System.out.println("The first file is uploaded successfully.");
        }
        inputStream.close();

    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.tapontikes.java.doUpload.java

public doUpload(String user, String pass, String path, String host) {

    _user = user;
    _pass = pass;
    _path = path;
    _host = host;
    _ftp = new FTPClient();

}

From source file:m09_uf3_7.ClientFTP.java

public ClientFTP(String server, int port, String user, String pass) throws IOException {
    this.server = server;
    this.port = port;
    this.user = user;
    this.pass = pass;

    ftp = new FTPClient();

    conectar();//from   w ww  . j a  va  2  s.  com

}