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:net.paissad.jcamstream.utils.FTPUtils.java

/**
 * Logout and then disconnect from the FTP server.
 * /*from www  .j  a va 2  s.c om*/
 * @throws IOException
 * 
 * @see org.apache.commons.net.ftp.FTPClient#logout()
 * @see org.apache.commons.net.ftp.FTPClient#disconnect()
 */
public void logoutAndDisconnect() throws IOException {
    FTPClient client = this.getFtpClient();

    if (client.isConnected() && !FTPReply.isPositiveIntermediate(client.getReplyCode())) {
        client.logout();
        client.disconnect();
        System.out.println("Disconnected successfully from FTP server.");
    }

    if (client.isConnected() && !client.completePendingCommand()) {
        System.err.println("Something failed !");
    }
}

From source file:com.cisco.dvbu.ps.utils.net.FtpFile.java

public void ftpFile(String fileName) throws CustomProcedureException, SQLException {

    // new ftp client
    FTPClient ftp = new FTPClient();
    OutputStream output = null;//from   w  w w. ja  va 2 s .c  o m

    success = false;
    try {
        //try to connect
        ftp.connect(hostIp);

        //login to server
        if (!ftp.login(userId, userPass)) {
            ftp.logout();
            qenv.log(LOG_ERROR, "Ftp server refused connection user/password incorrect.");
        }
        int reply = ftp.getReplyCode();

        //FTPReply stores a set of constants for FTP Reply codes
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            qenv.log(LOG_ERROR, "Ftp server refused connection.");
        }

        //enter passive mode
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE, FTPClient.BINARY_FILE_TYPE);
        ftp.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();

        //get system name
        //System.out.println("Remote system is " + ftp.getSystemType());

        //change current directory
        ftp.changeWorkingDirectory(ftpDirName);
        System.out.println("Current directory is " + ftp.printWorkingDirectory());
        System.out.println("File is " + fileName);

        output = new FileOutputStream(dirName + "/" + fileName);

        //get the file from the remote system
        success = ftp.retrieveFile(fileName, output);

        //close output stream
        output.close();

    } catch (IOException ex) {
        throw new CustomProcedureException("Error in CJP " + getName() + ": " + ex.toString());
    }
}

From source file:beans.BL.java

/**
 * Upload FTP Server/*ww w.  java2 s  .c  o m*/
 *
 * @param filename
 * @throws IOException
 */
public void upload(String filename) throws IOException {
    FTPClient client = new FTPClient();
    FileInputStream fis = null;

    client.connect("ftp.sunlime.at", 990);
    client.login("admin", "secret");

    fis = new FileInputStream(filename);
    client.storeFile(filename, fis);
    client.logout();
    fis.close();
}

From source file:hd3gtv.storage.AbstractFileBridgeFtpNexio.java

private FTPClient connectMe() throws IOException {
    FTPClient ftpclient = new FTPClient();
    ftpclient.connect(configurator.host, configurator.port);

    if (ftpclient.login(configurator.username, configurator.password) == false) {
        ftpclient.logout();
        throw new IOException("Can't login to server");
    }/*w  w  w  .  j  a  va2 s  .c o m*/
    int reply = ftpclient.getReplyCode();
    if (FTPReply.isPositiveCompletion(reply) == false) {
        ftpclient.disconnect();
        throw new IOException("Can't login to server");
    }

    ftpclient.setFileType(FTP.BINARY_FILE_TYPE);

    if (configurator.passive) {
        ftpclient.enterLocalPassiveMode();
    } else {
        ftpclient.enterLocalActiveMode();
    }
    ftpclient.changeWorkingDirectory("/" + path);
    if (ftpclient.printWorkingDirectory().equals("/" + path) == false) {
        throw new IOException("Can't change working dir : " + "/" + path);
    }

    return ftpclient;
}

From source file:cn.zhuqi.mavenssh.web.util.FTPClientTemplate.java

/**
 * ftp/* w ww  .  j  av  a 2 s.  c  o m*/
 *
 * @throws Exception
 */
public void disconnect() throws Exception {
    try {
        FTPClient ftpClient = getFTPClient();
        ftpClient.logout();
        if (ftpClient.isConnected()) {
            ftpClient.disconnect();
            ftpClient = null;
        }
    } catch (IOException e) {
        throw new Exception("Could not disconnect from server.", e);
    }
}

From source file:com.starr.smartbuilds.service.FileService.java

public String getFile(Build build, ServletContext sc)
        throws FileNotFoundException, IOException, ParseException {

    /*if (fileName == null || fileName.equals("") || fileText == null || fileText.equals("")) {
     resultMsg = "<font color='red'>Fail: File is not created!</font>";
     } else {//from   ww  w. ja  va  2s . c o m
     try {*/
    String path = sc.getRealPath("/");
    System.out.println(path);
    File dir = new File(path + "/builds");
    if (!dir.exists() || !dir.isDirectory()) {
        dir.mkdir();
    }

    String fileName = path + "/builds/" + build.getChampion().getKeyChamp() + build.getId() + ".json";
    File fileBuild = new File(fileName);
    if (!fileBuild.exists() || fileBuild.isDirectory()) {
        String file_data = buildService.buildData(build, buildService.parseBlocks(build.getBlocks()));
        fileBuild.createNewFile();
        FileOutputStream fos = new FileOutputStream(fileBuild, false);
        fos.write(file_data.getBytes());
        fos.close();
    }

    FTPClient client = new FTPClient();
    FileInputStream fis = null;

    try {
        client.connect("itelit.ftp.ukraine.com.ua");
        client.login("itelit_dor", "154896");

        // Create an InputStream of the file to be uploaded
        fis = new FileInputStream(fileBuild.getAbsolutePath());

        // Store file to server
        client.storeFile("builds/" + build.getChampion().getKeyChamp() + build.getId() + ".json", fis);
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*} catch (IOException ex) {
     resultMsg = "<font color='red'>Fail: File is not created!</font>";
     }
     }*/
    return "http://dor.it-elit.org/builds/" + build.getChampion().getKeyChamp() + build.getId() + ".json";
}

From source file:ddf.test.itests.catalog.TestFtp.java

private void disconnectClient(FTPClient client) {
    if (client != null && client.isConnected()) {
        try {/*www. ja  va2  s  .  c o m*/
            client.logout();
        } catch (IOException ioe) {
            // ignore
        }
        try {
            client.disconnect();
        } catch (IOException ioe) {
            // ignore
        }
    }
}

From source file:com.webarch.common.net.ftp.FtpService.java

/**
 * //  www.ja v a 2s  . c  o m
 *
 * @param fileName   ??
 * @param path       ftp?
 * @param fileStream ?
 * @return true/false ?
 */
public boolean uploadFile(String fileName, String path, InputStream fileStream) {
    boolean success = false;
    FTPClient ftpClient = new FTPClient();
    try {
        int replyCode;
        ftpClient.connect(url, port);
        ftpClient.login(userName, password);
        replyCode = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode)) {
            return false;
        }
        ftpClient.changeWorkingDirectory(path);
        ftpClient.storeFile(fileName, fileStream);
        fileStream.close();
        ftpClient.logout();
        success = true;
    } catch (IOException e) {
        logger.error("ftp?", e);
    } finally {
        if (ftpClient.isConnected()) {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                logger.error("ftp?", e);
            }
        }
    }
    return success;
}

From source file:net.audumla.climate.bom.BOMDataLoader.java

public FTPFile getFTPFile(String host, String location) throws IOException {
    FTPClient ftp = getFTPClient(host);
    try {//w ww  .ja  v  a  2s  . co  m
        synchronized (ftp) {
            FTPFile[] files = ftp.listFiles(location);
            if (files.length > 0) {
                return files[0];
            } else {
                return null;
            }
        }
    } catch (IOException e) {
        try {
            ftp.logout();
            ftp.disconnect();
        } catch (Exception ex) {
            LOG.error("Failure to close connection", ex);
        }
        throw new UnsupportedOperationException(
                "Error locating File " + host + location + " FTP Code -> " + ftp.getReplyCode(), e);
    } finally {
        /*            if (!ftp.completePendingCommand()) {
        ftp.logout();
        ftp.disconnect();
                    }
                    ftp.disconnect();
        */
    }
}

From source file:eu.ggnet.dwoss.misc.op.listings.FtpTransfer.java

/**
 * Uploads a some files to a remove ftp host.
 * <p/>/*w ww. j a va 2  s  .c o m*/
 * @param config  the config for he connection.
 * @param uploads the upload commands
 * @param monitor an optional monitor.
 * @throws java.net.SocketException
 * @throws java.io.IOException
 */
public static void upload(ConnectionConfig config, IMonitor monitor, UploadCommand... uploads)
        throws SocketException, IOException {
    if (uploads == null || uploads.length == 0)
        return;
    SubMonitor m = SubMonitor.convert(monitor, "FTP Transfer", toWorkSize(uploads) + 4);
    m.message("verbinde");
    m.start();
    FTPClient ftp = new FTPClient();
    ftp.addProtocolCommandListener(PROTOCOL_TO_LOGGER);

    try {
        ftp.connect(config.getHost(), config.getPort());
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode()))
            throw new IOException("FTPReply.isPositiveCompletion(ftp.getReplyCode()) = false");
        if (!ftp.login(config.getUser(), config.getPass()))
            throw new IOException("Login with " + config.getUser() + " not successful");
        L.info("Connected to {} idenfied by {}", config.getHost(), ftp.getSystemType());

        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        for (UploadCommand upload : uploads) {
            m.worked(1, "uploading to " + upload.getPath());
            ftp.changeWorkingDirectory(upload.getPath());
            deleteFilesByType(ftp, upload.getDeleteFileTypes());
            for (File file : upload.getFiles()) {
                m.worked(1, "uploading to " + upload.getPath() + " file " + file.getName());
                try (InputStream input = new FileInputStream(file)) {
                    if (!ftp.storeFile(file.getName(), input))
                        throw new IOException("Cannot store file " + file + " on server!");
                }
            }
        }
        m.finish();
    } finally {
        // just cleaning up
        try {
            ftp.logout();
        } catch (IOException e) {
        }
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
            }
        }
    }
}