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

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

Introduction

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

Prototype

public boolean logout() throws IOException 

Source Link

Document

Logout of the FTP server by sending the QUIT command.

Usage

From source file:mysynopsis.FTPUploader.java

public static String uploadWizard() throws IOException {

    FTPClient connect;
    connect = null;// ww w.  ja va  2s . c  o m

    try {
        connect = new FTPClient();
        connect.connect(server, 21);
        int reply = connect.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            connect.disconnect();
            return "Can't Connect to the Server";
        }

        boolean check = connect.login(user, pass);
        if (!check) {
            return "Username or password Incorrect";
        }

        connect.setFileType(FTP.BINARY_FILE_TYPE);
        connect.enterLocalPassiveMode();

        InputStream input;
        try {
            input = new FileInputStream(new File("index.html"));
            connect.storeFile(dir + "index.html", input);
            input.close();
            connect.logout();
            connect.disconnect();

        } catch (IOException ex) {
            return "You need to put a slash (/) at the end";
        }
    } catch (IOException iOException) {
        return "Wrong Server Information. Please Try again";
    }
    return "File Transfer Successful!";
}

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 {/*from   w  w w . j ava 2 s . c  o  m*/

        // 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:gui.TesteHackathon.java

public static void enviaImagem() {
    String server = "www.ejec.co";
    int port = 21;
    String user = "ejec";
    String pass = "cPanel2015";

    FTPClient ftpClient = new FTPClient();
    try {//from   w  w  w .  j  a v a  2s.  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("image.jpg");
        String firstRemoteFile = "public_html/virtualfit/image.jpg";
        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.");
        }

    } 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.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 {//from  w  ww.  j  av a  2 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:ftp.FTPtask.java

/**
 * Change directory//from w  w w. ja  v a 2 s. c o m
 * @param FTPADDR, ?  ?
  * @param user,   
  * @param password,   
  * @param ChangeFolder, ,     ,  /upload
 */
public static void FTPChangeDir(String FTPADDR, String user, String password, String ChangeFolder) {

    FTPClient client = new FTPClient();

    try {

        client.connect(FTPADDR);
        client.login(user, password);

        int replyCode = client.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode)) {
            System.out.println("Connect failed");
            return;
        }

        boolean success = client.login(user, password);
        showServerReply(client);

        if (!success) {
            System.out.println("Could not login to the server");
            return;
        }

        // Changes working directory
        success = client.changeWorkingDirectory(ChangeFolder);
        showServerReply(client);

        if (success) {
            System.out.println("Successfully changed working directory.");
        } else {
            System.out.println("Failed to change working directory. See server's reply.");
        }

        // logs out
        client.logout();
        client.disconnect();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:cn.zhuqi.mavenssh.web.util.test.FtpTest.java

/**
 * Description: FTP?//from  www.j  a va2s  . com
 *
 * @Version1.0
 * @param url FTP?hostname
 * @param port FTP??
 * @param username FTP?
 * @param password FTP?
 * @param remotePath FTP?
 * @param fileName ???
 * @param localPath ??
 * @return
 */
public static boolean downloadFile(String url, int port, String username, String password, String remotePath,
        String fileName, String localPath) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        // FTP?
        if (port > -1) {
            ftp.connect(url, port);
        } else {
            ftp.connect(url);
        }
        ftp.login(username, password);//
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        ftp.changeWorkingDirectory(remotePath);//FTP?
        FTPFile[] fs = ftp.listFiles();
        for (FTPFile ff : fs) {
            if (ff.getName().equals(fileName)) {
                File localFile = new File(localPath + "/" + ff.getName());
                OutputStream is = new FileOutputStream(localFile);
                ftp.retrieveFile(ff.getName(), is);
                is.close();
            }
        }

        ftp.logout();
        success = true;
    } catch (IOException e) {
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException e) {
            }
        }
    }
    return success;
}

From source file:ca.nrc.cadc.web.ServerToServerFTPTransfer.java

/**
 *
 * @param ftpClient     The Connected client.
 * @return              The mutated client, now authenticated.
 * @throws AccessControlException    If login fails.
 *///from w  ww  . ja  v  a 2 s  .com
private FTPClient login(final FTPClient ftpClient) throws AccessControlException, IOException {
    if (!ftpClient.login(CREDENTIAL, CREDENTIAL)) {
        ftpClient.logout();
        throw new AccessControlException(String.format("Login failed for %s", CREDENTIAL));
    } else {
        return ftpClient;
    }
}

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

public void uploadMutiFileWithFTP(String targetDirectory, FileTransferController fileTransferController) {
    try {/*from ww w.j  a v a 2  s. c om*/
        FtpProperties properties = new ResourcesProperties().loadFTPProperties();

        FTPClient ftp = new FTPClient();
        ftp.connect(properties.getFtp_server());
        if (!ftp.login(properties.getFtp_username(), properties.getFtp_password())) {
            ftp.logout();
        }
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
        }
        ftp.enterLocalPassiveMode();
        System.out.println("Remote system is " + ftp.getSystemName());
        ftp.changeWorkingDirectory(targetDirectory);
        System.out.println("Current directory is " + ftp.printWorkingDirectory());
        FTPFile[] ftpFiles = ftp.listFiles();
        if (ftpFiles != null && ftpFiles.length > 0) {
            for (FTPFile file : ftpFiles) {
                if (!file.isFile()) {
                    continue;
                }
                System.out.println("File is " + file.getName());
                OutputStream output;
                output = new FileOutputStream(
                        properties.getFtp_remote_directory() + File.separator + file.getName());
                ftp.retrieveFile(file.getName(), output);
                output.close();
            }
        }
        ftp.logout();
        ftp.disconnect();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:autonomouspathplanner.ftp.FTP.java

/**
 * Attempts to connect to server to check if it is possible
 * @return true if the client can connect to the server and false otherwise.
 *///from   w w  w  .  ja v a  2 s  .  co m
public boolean canConnect() {
    try {
        FTPClient c = new FTPClient();
        c.connect(IP, port);

        c.login(user, pass);
        if (c.isConnected()) {
            c.logout();
            c.disconnect();
            return true;
        } else
            return false;
    } catch (UnknownHostException x) {
        return false;
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    }

}

From source file:EscribirCorreo.java

private void SubirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubirActionPerformed
    //Codigo para subir archivo         
    String localfile = subir1Field.getText();
    String server = "51.254.137.26";
    String username = "proyecto";
    String password = "proyecto";
    String destinationfile = nombre;
    try {//from  w  w  w .  j  a  v  a 2 s.c o  m
        FTPClient ftp = new FTPClient();
        ftp.connect(server);
        if (!ftp.login(username, password)) {
            ftp.logout();
        }
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
        }
        InputStream in = new FileInputStream(localfile);
        ftp.setFileType(ftp.BINARY_FILE_TYPE);
        ftp.storeFile(destinationfile, in);
        JOptionPane.showMessageDialog(null, "Archivo subido correctamente", "Subido!",
                JOptionPane.INFORMATION_MESSAGE);
        if (archivos == null) {
            archivos = "http://51.254.137.26/proyecto/" + destinationfile;
        } else {
            archivos = archivos + "#http://51.254.137.26/proyecto/" + destinationfile;
        }
        in.close();
        ftp.logout();
        ftp.disconnect();

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

}