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:org.jumpmind.metl.core.runtime.resource.FtpDirectory.java

protected void close(FTPClient ftpClient) {
    if (ftpClient != null) {
        try {/*from  ww w.  j  a  v a  2s. c  o m*/
            ftpClient.logout();
        } catch (IOException e) {
        }

        try {
            ftpClient.disconnect();
        } catch (IOException e) {
        }
    }
}

From source file:org.kuali.kfs.module.cg.service.impl.CfdaServiceImpl.java

/**
 * @return/*  ww  w .j  a va  2 s.c  o  m*/
 * @throws IOException
 */
public SortedMap<String, CFDA> getGovCodes() throws IOException {
    Calendar calendar = dateTimeService.getCurrentCalendar();
    SortedMap<String, CFDA> govMap = new TreeMap<String, CFDA>();

    // ftp://ftp.cfda.gov/programs09187.csv
    String govURL = parameterService.getParameterValueAsString(CfdaBatchStep.class,
            KFSConstants.SOURCE_URL_PARAMETER);
    String fileName = StringUtils.substringAfterLast(govURL, "/");
    govURL = StringUtils.substringBeforeLast(govURL, "/");
    if (StringUtils.contains(govURL, "ftp://")) {
        govURL = StringUtils.remove(govURL, "ftp://");
    }

    // need to pull off the '20' in 2009
    String year = "" + calendar.get(Calendar.YEAR);
    year = year.substring(2, 4);
    fileName = fileName + year;

    // the last 3 numbers in the file name are the day of the year, but the files are from "yesterday"
    fileName = fileName + String.format("%03d", calendar.get(Calendar.DAY_OF_YEAR) - 1);
    fileName = fileName + ".csv";

    LOG.info("Getting government file: " + fileName + " for update");

    InputStream inputStream = null;
    FTPClient ftp = new FTPClient();
    try {
        ftp.connect(govURL);
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            LOG.error("FTP connection to server not established.");
            throw new IOException("FTP connection to server not established.");
        }

        boolean isLoggedIn = ftp.login("anonymous", "");
        if (!isLoggedIn) {
            LOG.error("Could not login as anonymous.");
            throw new IOException("Could not login as anonymous.");
        }

        LOG.info("Successfully connected and logged in");
        ftp.enterLocalPassiveMode();
        inputStream = ftp.retrieveFileStream(fileName);
        if (inputStream != null) {
            LOG.info("reading input stream");
            InputStreamReader screenReader = new InputStreamReader(inputStream);
            BufferedReader screen = new BufferedReader(screenReader);

            CSVReader csvReader = new CSVReader(screenReader, ',', '"', 1);
            List<String[]> lines = csvReader.readAll();
            for (String[] line : lines) {
                String title = line[0];
                String number = line[1];

                CFDA cfda = new CFDA();
                cfda.setCfdaNumber(number);
                cfda.setCfdaProgramTitleName(title);

                govMap.put(number, cfda);
            }
        }

        ftp.logout();
        ftp.disconnect();
    } finally {
        if (ftp.isConnected()) {
            ftp.disconnect();
        }
    }

    return govMap;
}

From source file:org.kuali.kra.external.Cfda.service.impl.CfdaServiceImpl.java

/**
 * This method disconnects from server./*w  w w  .  jav  a2  s  . co  m*/
 * @param ftp
 * @throws IOException
 */
public void disconnect(FTPClient ftp) throws IOException {
    ftp.logout();
    if (ftp.isConnected()) {
        ftp.disconnect();
    }
}

From source file:org.kuali.ole.coa.service.impl.CfdaServiceImpl.java

/**
 * @return/*from   w w  w  .  j a va  2s. c o  m*/
 * @throws IOException
 */
public SortedMap<String, CFDA> getGovCodes() throws IOException {
    Calendar calendar = SpringContext.getBean(DateTimeService.class).getCurrentCalendar();
    SortedMap<String, CFDA> govMap = new TreeMap<String, CFDA>();

    // ftp://ftp.cfda.gov/programs09187.csv
    String govURL = SpringContext.getBean(ParameterService.class).getParameterValueAsString(CfdaBatchStep.class,
            OLEConstants.SOURCE_URL_PARAMETER);
    String fileName = StringUtils.substringAfterLast(govURL, "/");
    govURL = StringUtils.substringBeforeLast(govURL, "/");
    if (StringUtils.contains(govURL, "ftp://")) {
        govURL = StringUtils.remove(govURL, "ftp://");
    }

    // need to pull off the '20' in 2009
    String year = "" + calendar.get(Calendar.YEAR);
    year = year.substring(2, 4);
    fileName = fileName + year;

    // the last 3 numbers in the file name are the day of the year, but the files are from "yesterday"
    fileName = fileName + String.format("%03d", calendar.get(Calendar.DAY_OF_YEAR) - 1);
    fileName = fileName + ".csv";

    LOG.info("Getting government file: " + fileName + " for update");

    InputStream inputStream = null;
    FTPClient ftp = new FTPClient();
    try {
        ftp.connect(govURL);
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            LOG.error("FTP connection to server not established.");
            throw new IOException("FTP connection to server not established.");
        }

        boolean loggedIn = ftp.login("anonymous", "");
        if (!loggedIn) {
            LOG.error("Could not login as anonymous.");
            throw new IOException("Could not login as anonymous.");
        }

        LOG.info("Successfully connected and logged in");
        ftp.enterLocalPassiveMode();
        inputStream = ftp.retrieveFileStream(fileName);
        if (inputStream != null) {
            LOG.info("reading input stream");
            InputStreamReader screenReader = new InputStreamReader(inputStream);
            BufferedReader screen = new BufferedReader(screenReader);

            CSVReader csvReader = new CSVReader(screenReader, ',', '"', 1);
            List<String[]> lines = csvReader.readAll();
            for (String[] line : lines) {
                String title = line[0];
                String number = line[1];

                CFDA cfda = new CFDA();
                cfda.setCfdaNumber(number);
                cfda.setCfdaProgramTitleName(title);

                govMap.put(number, cfda);
            }
        }

        ftp.logout();
        ftp.disconnect();
    } finally {
        if (ftp.isConnected()) {
            ftp.disconnect();
        }
    }

    return govMap;
}

From source file:org.limy.common.util.FtpUtils.java

/**
 * FTP???//from  w w w.  j  ava  2s .c  o  m
 * @param client FTP
 * @throws IOException I/O
 */
public static void disconnect(FTPClient client) throws IOException {
    client.disconnect();
}

From source file:org.mockftpserver.stub.example.RemoteFile.java

public String readFile(String filename) throws SocketException, IOException {

    FTPClient ftpClient = new FTPClient();
    ftpClient.connect(server, port);//from  w w w .  j a  v a2 s .  c  o  m

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    boolean success = ftpClient.retrieveFile(filename, outputStream);
    ftpClient.disconnect();

    if (!success) {
        throw new IOException("Retrieve file failed: " + filename);
    }
    return outputStream.toString();
}

From source file:org.mousephenotype.dcc.crawler.Downloader.java

private void closeOpenConnections() {
    // close all ftp connections
    Iterator<Entry<String, FTPClient>> ci = ftpConnections.entrySet().iterator();
    while (ci.hasNext()) {
        Entry<String, FTPClient> item = ci.next();
        FTPClient c = item.getValue();
        if (c.isConnected()) {
            try {
                c.logout();/* w w  w.jav a2 s.co m*/
                c.disconnect();
                logger.debug("Downloader has disconnected from '{}'", item.getKey());
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }
        ci.remove();
    }

    // close all sftp channels (before closing sessions)
    Iterator<Entry<String, ChannelSftp>> ch = sftpChannels.entrySet().iterator();
    while (ch.hasNext()) {
        Entry<String, ChannelSftp> item = ch.next();
        ChannelSftp c = item.getValue();
        if (c.isConnected()) {
            c.disconnect();
            logger.debug("Downloader has closed sftp channel with '{}'", item.getKey());
        }
        ch.remove();
    }

    // close all sftp sessions
    Iterator<Entry<String, Session>> se = sftpSessions.entrySet().iterator();
    while (se.hasNext()) {
        Entry<String, Session> item = se.next();
        Session c = item.getValue();
        if (c.isConnected()) {
            c.disconnect();
            logger.debug("Downloader has closed sftp session with '{}'", item.getKey());
        }
        se.remove();
    }
}

From source file:org.mule.modules.FtpUtils.java

public static void disconnect(FTPClient client) {
    try {/*from w  w  w .  ja  va 2  s  .com*/
        client.disconnect();
    } catch (IOException e) {
        throw new FtpLiteException(e.toString());
    }

}

From source file:org.mule.modules.FtpUtils.java

public static FTPFile[] listFiles(FTPClient client, String path) {
    try {//from   w  ww.  ja v  a  2  s  . c o  m
        if (path == null || path.isEmpty()) {
            path = client.printWorkingDirectory();
        }
        return client.listFiles(path);
    } catch (IOException e) {
        try {
            client.disconnect();
        } catch (Exception error) {
            throw new FtpLiteException("There was an error disconnecting. " + error.toString());
        }
        throw new FtpLiteException("There was an error fetching files from SFTP");
    }
}

From source file:org.mule.transport.ftp.FtpConnectionFactory.java

public void destroyObject(Object obj) throws Exception {
    FTPClient client = (FTPClient) obj;
    client.logout();//from  www.j  a v a2s .c om
    client.disconnect();
}