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:org.kuali.ole.coa.service.impl.CfdaServiceImpl.java

/**
 * @return/*  w  w  w  . j av a 2s.c  om*/
 * @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.kuali.ole.module.purap.transmission.service.impl.TransmissionServiceImpl.java

/**
 * This method is to perform file upload
 *
 * @param ftpHostname/*from   ww w  .  j ava  2 s .c o m*/
 * @param ftpUsername
 * @param ftpPassword
 * @param file
 * @param fileName
 */
@Override
public void doFTPUpload(String ftpHostname, String ftpUsername, String ftpPassword, String file,
        String fileName) {
    LOG.trace("************************************doFTPUpload() started************************************");
    FTPClient ftpClient = new FTPClient();
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
    try {
        ftpClient.connect(ftpHostname);
        ftpClient.login(ftpUsername, ftpPassword);
        ftpClient.enterLocalPassiveMode();
        int reply = ftpClient.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            LOG.debug("Connected to FTP server.");
        } else {
            LOG.debug("FTP server refused connection.");
        }

        // upload
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        String fileLocation = getFileLocation();
        if (LOG.isDebugEnabled()) {
            LOG.debug("File Location in FTP Server================>" + fileLocation);
            LOG.debug("File source=================================>" + file);
            LOG.debug("FileName====================================>" + fileName);
        }
        ftpClient.mkd(fileLocation);
        ftpClient.cwd(fileLocation);
        inputStream = new FileInputStream(file);
        ftpClient.storeFile(fileName, inputStream);

        ftpClient.logout();
        inputStream.close();
    } catch (Exception e) {
        LOG.error("Exception performing SFTP upload of " + file + " to " + ftpHostname, e);
        throw new RuntimeException(e);
    }
    LOG.trace(
            "************************************doFTPUpload() completed************************************");
}

From source file:org.limy.lrd.deploy.LrdFtpDeployerImpl.java

/**
 * FTP????//www.j a va  2s.  c o m
 * @param repositoryBean ?Bean
 * @param session Web
 * @param ftpDeployInfo FTP
 * @param deployer 
 * @param deployInfo 
 * @throws IOException I/O
 * @throws LrdException 
 */
private void deployWithFtp(RepositoryBean repositoryBean, LimyWebSession session, LrdDeployInfo ftpDeployInfo,
        LrdDeployer deployer, Object deployInfo) throws IOException, LrdException {

    FTPClient client = new FTPClient();
    try {

        // FTP?
        FtpUtils.connectFtp(client, ftpDeployInfo.getFtpInfo());

        Context context = createContext(repositoryBean, ftpDeployInfo.getDeployUrl());

        Deployable deployable = new FtpDeployer(repositoryBean, client, ftpDeployInfo.getFtpInfo(), session);
        deployable.setAllDeployCount(countAllDeployFiles(repositoryBean));
        deployStaticFiles(repositoryBean, deployable, true);

        deployer.deploy(repositoryBean, context, deployable, deployInfo);
    } finally {
        try {
            FtpUtils.disconnect(client);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }

}

From source file:org.martin.ftp.net.FTPLinker.java

/**
 * /*w ww .j av a2 s. co m*/
 * @param server Servidor ftp al cual conectarse
 * @param user Usuario ftp
 * @param password Constrasea del usuario, si el usuario
 * no tiene contrasea este parametro puede ser nulo
 * @throws IOException Excepcion si hay problemas de conexion
 */

public FTPLinker(String server, String user, String password) throws IOException {

    this.server = server;
    this.user = user;
    this.password = password;
    if (this.password == null)
        this.password = "";

    client = new FTPClient();
    client.connect(server);
    client.login(user, password);
    client.setFileType(FTPClient.BINARY_FILE_TYPE);
}

From source file:org.middleheaven.io.repository.ftp.TestFTP.java

private FTPClient connect() {
    try {/*from  w  w w.  j ava 2  s  .  com*/
        FTPClient ftp = new FTPClient();
        if (!ftp.isConnected()) {
            ftp.connect("184.107.197.226");
            ftp.login("javabuil", "%Q&4pr|!O}=");
        }

        return ftp;
    } catch (IOException e) {
        throw ManagedIOException.manage(e);
    }
}

From source file:org.mockftpserver.core.server.AbstractFtpServer_StartTest.java

/**
 * Test setting a non-default port number for the StubFtpServer control connection socket.
 *///ww  w  .ja v  a 2 s  . c o m
public void testCustomServerControlPort() throws Exception {
    final int SERVER_CONTROL_PORT = 9187;

    ftpServer.setServerControlPort(SERVER_CONTROL_PORT);
    ftpServer.start();

    try {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(SERVER, SERVER_CONTROL_PORT);
    } finally {
        ftpServer.stop();
    }
}

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

/**
 * Return the current working directory for the FTP account on the server
 * @return the current working directory
 * @throws SocketException//from  w w w  .  ja v a  2  s.  com
 * @throws IOException
 */
public String getWorkingDirectory() throws SocketException, IOException {
    FTPClient ftpClient = new FTPClient();
    ftpClient.connect(server, port);
    return ftpClient.printWorkingDirectory();
}

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   ww  w . j  a va2  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.mockftpserver.stub.example.SpringConfigurationTest.java

/**
 * @see org.mockftpserver.test.AbstractTest#setUp()
 *///w ww.  j a  va2  s  .c o m
protected void setUp() throws Exception {
    super.setUp();

    ApplicationContext context = new ClassPathXmlApplicationContext("stubftpserver-beans.xml");
    stubFtpServer = (StubFtpServer) context.getBean("stubFtpServer");

    ftpClient = new FTPClient();
}

From source file:org.mockftpserver.stub.StubFtpServerIntegrationTest.java

/**
 * Perform initialization before each test
 * @see org.mockftpserver.test.AbstractTest#setUp()
 *//*from w w  w .jav a 2 s. co m*/
protected void setUp() throws Exception {
    super.setUp();

    for (int i = 0; i < BINARY_CONTENTS.length; i++) {
        BINARY_CONTENTS[i] = (byte) i;
    }

    stubFtpServer = new StubFtpServer();
    stubFtpServer.setServerControlPort(PortTestUtil.getFtpServerControlPort());
    stubFtpServer.start();
    ftpClient = new FTPClient();
    retrCommandHandler = (RetrCommandHandler) stubFtpServer.getCommandHandler(CommandNames.RETR);
    storCommandHandler = (StorCommandHandler) stubFtpServer.getCommandHandler(CommandNames.STOR);
}