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

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

Introduction

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

Prototype

@Override
public void disconnect() throws IOException 

Source Link

Document

Closes the connection to the FTP server and restores connection parameters to the default values.

Usage

From source file:ftp.FTPtask.java

/**
 * Change directory/*from   w w  w .j  a  v a  2  s.c  om*/
 * @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:lucee.runtime.net.ftp.FTPPoolImpl.java

/**
 * disconnect a client//from  w w w.  ja  v  a2s . c  o  m
 * @param client
 */
private void disconnect(FTPClient client) {
    try {
        if (client != null && client.isConnected()) {
            client.quit();
            client.disconnect();
        }
    } catch (IOException ioe) {
    }
}

From source file:de.ep3.ftpc.controller.portal.CrawlerDownloadController.java

private void tryDisconnect(FTPClient ftpClient) {
    if (ftpClient.isConnected()) {
        try {//from ww w.j  a v  a2s.c  o  m
            ftpClient.disconnect();
        } catch (IOException e) {
        }
    }
}

From source file:it.zero11.acme.example.FTPChallengeListener.java

private boolean createChallengeFiles(String token, String challengeBody) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {// w w w .  j  ava 2s  . c o m
        ftp.connect(host);
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            ftp.disconnect();
            return false;
        }

        ftp.login(username, password);
        ftp.changeWorkingDirectory(webroot);
        ftp.makeDirectory(".well-known");
        ftp.changeWorkingDirectory(".well-known");
        ftp.makeDirectory("acme-challenge");
        ftp.changeWorkingDirectory("acme-challenge");
        ftp.enterLocalPassiveMode();
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE, FTPClient.BINARY_FILE_TYPE);
        ftp.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
        success = ftp.storeFile(token, new ByteArrayInputStream(challengeBody.getBytes()));
        if (!success)
            System.err.println("FTP error uploading file: " + ftp.getReplyCode() + ": " + ftp.getReplyString());
        ftp.logout();
    } catch (IOException e) {
        throw new AcmeException(e);
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
            }
        }
    }

    return success;
}

From source file:it.zero11.acme.example.FTPChallengeListener.java

private void deleteChallengeFiles() {
    FTPClient ftp = new FTPClient();
    try {/*from   ww w  .  j  a  va2  s.  c om*/
        ftp.connect(host);
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            ftp.disconnect();
            return;
        }

        ftp.login(username, password);
        ftp.changeWorkingDirectory(webroot);
        ftp.changeWorkingDirectory(".well-known");
        ftp.changeWorkingDirectory("acme-challenge");

        FTPFile[] subFiles = ftp.listFiles();

        if (subFiles != null && subFiles.length > 0) {
            for (FTPFile aFile : subFiles) {
                String currentFileName = aFile.getName();
                if (currentFileName.equals(".") || currentFileName.equals("..")) {
                    continue;
                } else {
                    ftp.deleteFile(currentFileName);
                }
            }
        }
        ftp.changeToParentDirectory();
        ftp.removeDirectory("acme-challenge");
        ftp.changeToParentDirectory();
        ftp.removeDirectory(".well-known");
        ftp.logout();
    } catch (IOException e) {
        throw new AcmeException(e);
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:br.com.atmatech.painel.util.EnviaLogs.java

public void enviarBD(File file, String enderecoFTP, String login, String senha) throws IOException {
    String nomeArquivo = null;/*from   ww w . ja  va 2s. c o m*/
    FTPClient ftp = new FTPClient();
    ftp.connect(enderecoFTP);
    //verifica se conectou com sucesso! 

    if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
        ftp.login(login, senha);
    } else {
        //erro ao se conectar  
        ftp.disconnect();
    }
    ftp.changeWorkingDirectory("/atmatech.com.br/ErroLogsPainelFX/");
    //abre um stream com o arquivo a ser enviado              
    InputStream is = new FileInputStream(file);
    //pega apenas o nome do arquivo             
    int idx = file.getName().lastIndexOf(File.separator);
    if (idx < 0)
        idx = 0;
    else
        idx++;
    nomeArquivo = file.getName().substring(idx, file.getName().length());
    //ajusta o tipo do arquivo a ser enviado  
    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
    //faz o envio do arquivo  
    ftp.storeFile(nomeArquivo, is);
    ftp.disconnect();

}

From source file:de.cismet.cids.custom.utils.formsolutions.FormSolutionFtpClient.java

/**
 * DOCUMENT ME!/*from   w  ww  .ja v  a  2s  .  co  m*/
 *
 * @return  DOCUMENT ME!
 *
 * @throws  Exception  DOCUMENT ME!
 */
private FTPClient getConnectedFTPClient() throws Exception {
    final FTPClient ftpClient = new FTPClient();
    ftpClient.connect(FormSolutionsProperties.getInstance().getFtpHost());

    final int reply = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftpClient.disconnect();
        throw new Exception("Exception in connecting to FTP Server");
    }
    ftpClient.login(FormSolutionsProperties.getInstance().getFtpLogin(),
            FormSolutionsProperties.getInstance().getFtpPass());
    return ftpClient;
}

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

private FTPClient connect() throws IOException {
    final FTPClient ftpClient = createFTPClient();
    ftpClient.connect(hostname, port);/*w w w. j  a v a 2  s  .c o m*/

    final int reply = ftpClient.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply)) {
        ftpClient.disconnect();
        throw new IOException(String.format("FTP Connection failed with error code %d.", reply));
    } else {
        return login(ftpClient);
    }
}

From source file:com.jaeksoft.searchlib.crawler.file.process.fileInstances.FtpFileInstance.java

private void ftpQuietDisconnect(FTPClient f) {
    if (f == null)
        return;//from   w ww.j a  v a 2  s  .c  o  m
    try {
        if (f.isConnected())
            f.disconnect();
    } catch (IOException e) {
        Logging.warn(e);
    }
}

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 ww w  . ja va2  s. c om*/
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;
    }

}