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

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

Introduction

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

Prototype

public InputStream retrieveFileStream(String remote) throws IOException 

Source Link

Document

Returns an InputStream from which a named file from the server can be read.

Usage

From source file:org.gogpsproject.parser.sp3.SP3Navigation.java

private SP3Parser getFromFTP(String url) throws IOException {
    SP3Parser sp3p = null;/*from  www. j  a  va2 s .  co  m*/

    String filename = url.replaceAll("[ ,/:]", "_");
    if (filename.endsWith(".Z"))
        filename = filename.substring(0, filename.length() - 2);
    File sp3f = new File(SP3_CACHE, filename);

    if (!sp3f.exists()) {
        System.out.println(url + " from the net.");
        FTPClient ftp = new FTPClient();

        try {
            int reply;
            System.out.println("URL: " + url);
            url = url.substring("ftp://".length());
            String server = url.substring(0, url.indexOf('/'));
            String remoteFile = url.substring(url.indexOf('/'));
            String remotePath = remoteFile.substring(0, remoteFile.lastIndexOf('/'));
            remoteFile = remoteFile.substring(remoteFile.lastIndexOf('/') + 1);

            ftp.connect(server);
            ftp.login("anonymous", "info@eriadne.org");

            System.out.print(ftp.getReplyString());

            // After connection attempt, you should check the reply code to
            // verify
            // success.
            reply = ftp.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                System.err.println("FTP server refused connection.");
                return null;
            }

            System.out.println("cwd to " + remotePath + " " + ftp.changeWorkingDirectory(remotePath));
            System.out.println(ftp.getReplyString());
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            System.out.println(ftp.getReplyString());

            System.out.println("open " + remoteFile);
            InputStream is = ftp.retrieveFileStream(remoteFile);
            InputStream uis = is;
            System.out.println(ftp.getReplyString());
            if (ftp.getReplyString().startsWith("550")) {
                throw new FileNotFoundException();
            }

            if (remoteFile.endsWith(".Z")) {
                uis = new UncompressInputStream(is);
            }

            sp3p = new SP3Parser(uis, sp3f);
            sp3p.init();
            is.close();

            ftp.completePendingCommand();

            ftp.logout();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                    // do nothing
                }
            }
        }
    } else {
        System.out.println(url + " from cache file " + sp3f);
        sp3p = new SP3Parser(sp3f);
        sp3p.init();
    }
    return sp3p;
}

From source file:org.jumpmind.metl.core.runtime.resource.FtpDirectory.java

@Override
public InputStream getInputStream(String relativePath, boolean mustExist) {
    FTPClient ftpClient = null;
    try {/*from w w  w.ja v  a  2 s.  c  om*/
        ftpClient = createClient();
        InputStream is = ftpClient.retrieveFileStream(relativePath);
        if (is != null) {
            return new CloseableInputStreamStream(is, ftpClient);
        } else {
            if (!mustExist) {
                String msg = String.format("Failed to open %s.  The ftp return code was %s", relativePath,
                        ftpClient.getReplyCode());
                throw new IoException(msg);
            } else {
                return null;
            }
        }
    } catch (Exception e) {
        throw new IoException(e);
    }
}

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

/**
 * @return/*w ww  . j  a v a  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 retrieves cfda codes from the government site
 * @return/*from   w ww .java 2 s.c  o m*/
 * @throws IOException
 */
public SortedMap<String, CFDA> retrieveGovCodes() throws IOException {

    SortedMap<String, CFDA> govMap = new TreeMap<String, CFDA>();
    createGovURL();
    LOG.info("Getting government file: " + cfdaFileName + " from URL " + govURL + " for update");

    InputStream inputStream = null;
    FTPClient ftp = connect(getGovURL());
    try {
        inputStream = ftp.retrieveFileStream(cfdaFileName);
        if (inputStream != null) {
            LOG.info("reading input stream");
            InputStreamReader screenReader = new InputStreamReader(inputStream);
            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);
                cfda.setCfdaMaintenanceTypeId(Constants.CFDA_MAINT_TYP_ID_AUTOMATIC);
                govMap.put(number, cfda);
            }
        } else {
            // If file name is incorrect
            throw new IOException("Input stream is null. The file " + cfdaFileName
                    + " could not be retrieved from " + govURL);
        }
    } finally {
        disconnect(ftp);
    }
    return govMap;
}

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

/**
 * @return/*from  ww w . ja  v  a2 s .  co  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.pepstock.jem.node.resources.impl.ftp.FtpFactory.java

/**
 * Creates and configures a FtpClient instance based on the
 * given properties./* w  w w  . j  a va  2  s. com*/
 * 
 * @param properties the ftp client configuration properties
 * @return remote input/output steam
 * @throws JNDIException if an error occurs creating the ftp client
 */
private Object createFtpClient(Properties properties) throws JNDIException {
    // URL is mandatory
    String ftpUrlString = properties.getProperty(CommonKeys.URL);
    if (ftpUrlString == null) {
        throw new JNDIException(NodeMessage.JEMC136E, CommonKeys.URL);
    }

    // creates URL objects
    // from URL string
    URL ftpUrl;
    try {
        ftpUrl = new URL(ftpUrlString);
    } catch (MalformedURLException e) {
        throw new JNDIException(NodeMessage.JEMC233E, e, ftpUrlString);
    }
    // checks scheme
    // if SSL, activates a FTPS
    if (!ftpUrl.getProtocol().equalsIgnoreCase(FTP_PROTOCOL)
            && !ftpUrl.getProtocol().equalsIgnoreCase(FTPS_PROTOCOL)) {
        throw new JNDIException(NodeMessage.JEMC137E, ftpUrl.getProtocol());
    }

    // gets port the host (from URL)
    int port = ftpUrl.getPort();
    String server = ftpUrl.getHost();

    // User id is mandatory
    String username = properties.getProperty(CommonKeys.USERID);
    if (username == null) {
        throw new JNDIException(NodeMessage.JEMC136E, CommonKeys.USERID);
    }

    // password is mandatory
    String password = properties.getProperty(CommonKeys.PASSWORD);
    if (password == null) {
        throw new JNDIException(NodeMessage.JEMC136E, CommonKeys.PASSWORD);
    }

    // checks if as input stream or not
    boolean asInputStream = Parser
            .parseBoolean(properties.getProperty(FtpResourceKeys.AS_INPUT_STREAM, "false"), false);

    String remoteFile = null;
    String accessMode = null;
    if (asInputStream) {
        // remote file is mandatory
        // it must be set by a data description
        remoteFile = properties.getProperty(FtpResourceKeys.REMOTE_FILE);
        if (remoteFile == null) {
            throw new JNDIException(NodeMessage.JEMC136E, FtpResourceKeys.REMOTE_FILE);
        }
        // access mode is mandatory
        // it must be set by a data description
        accessMode = properties.getProperty(FtpResourceKeys.ACTION_MODE, FtpResourceKeys.ACTION_READ);
    }

    // creates a FTPclient 
    FTPClient ftp = ftpUrl.getProtocol().equalsIgnoreCase(FTP_PROTOCOL) ? new FTPClient() : new FTPSClient();

    // checks if binary
    boolean binaryTransfer = Parser.parseBoolean(properties.getProperty(FtpResourceKeys.BINARY, "false"),
            false);

    // checks if must be restarted
    long restartOffset = Parser.parseLong(properties.getProperty(FtpResourceKeys.RESTART_OFFSET, "-1"), -1);

    // checks and sets buffer size
    int bufferSize = Parser.parseInt(properties.getProperty(FtpResourceKeys.BUFFER_SIZE, "-1"), -1);

    try {
        // reply code instance
        int reply;
        // connect to server
        if (port > 0) {
            ftp.connect(server, port);
        } else {
            ftp.connect(server);
        }

        // After connection attempt, you should check the reply code to
        // verify
        // success.
        reply = ftp.getReplyCode();
        // if not connected for error, EXCEPTION
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new JNDIException(NodeMessage.JEMC138E, reply);
        }
        // login!!
        if (!ftp.login(username, password)) {
            ftp.logout();
        }
        // set all ftp properties
        if (binaryTransfer) {
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
        }

        // sets restart offset if has been set
        if (restartOffset >= 0) {
            ftp.setRestartOffset(restartOffset);
        }

        // sets buffer size
        if (bufferSize >= 0) {
            ftp.setBufferSize(bufferSize);
        }

        // if is not related to a data descritpion,
        // returns a FTP object
        if (!asInputStream) {
            return new Ftp(ftp);
        }

        // checks if is in WRITE mode
        if (accessMode.equalsIgnoreCase(FtpResourceKeys.ACTION_WRITE)) {
            // gets outputstream
            // using the file name passed by data descritpion
            OutputStream os = ftp.storeFileStream(remoteFile);
            if (os == null) {
                reply = ftp.getReplyCode();
                throw new JNDIException(NodeMessage.JEMC206E, remoteFile, reply);
            }
            // returns outputstream
            return new FtpOutputStream(os, ftp);
        } else {
            // gest inputstream
            // using the file name passed by data descritpion
            InputStream is = ftp.retrieveFileStream(remoteFile);
            if (is == null) {
                reply = ftp.getReplyCode();
                throw new JNDIException(NodeMessage.JEMC206E, remoteFile, reply);
            }
            // returns inputstream 
            return new FtpInputStream(is, ftp);
        }
    } catch (SocketException e) {
        throw new JNDIException(NodeMessage.JEMC234E, e);
    } catch (IOException e) {
        throw new JNDIException(NodeMessage.JEMC234E, e);
    }
}

From source file:org.ramadda.repository.type.FtpTypeHandler.java

/**
 * _more_/*  w ww. j a  v a 2  s . c  om*/
 *
 * @param request _more_
 * @param mainEntry _more_
 * @param parentEntry _more_
 * @param synthId _more_
 *
 * @return _more_
 *
 * @throws Exception _more_
 */
public List<String> getSynthIds(Request request, Entry mainEntry, Entry parentEntry, String synthId)
        throws Exception {
    long t0 = System.currentTimeMillis();
    List<String> ids = new ArrayList<String>();

    Object[] values = mainEntry.getValues();
    String baseDir = (String) values[COL_BASEDIR];
    String path = getPathFromId(synthId, baseDir);

    /*        boolean descending = !request.get(ARG_ASCENDING, false);
    if (request.getString(ARG_ORDERBY, "").equals("name")) {
    files = IOUtil.sortFilesOnName(files, descending);
    } else {
    files = IOUtil.sortFilesOnAge(files, descending);
    }*/
    long t1 = System.currentTimeMillis();

    FTPClient ftpClient = getFtpClient(mainEntry);
    if (ftpClient == null) {
        return ids;
    }
    long t2 = System.currentTimeMillis();
    //        System.err.println ("getFtpClient:" + (t2-t1));

    try {
        String pattern = (String) values[COL_FILE_PATTERN];
        if ((pattern != null) && (pattern.trim().length() == 0)) {
            pattern = null;
        }
        boolean isDir = ftpClient.changeWorkingDirectory(path);
        if (isDir) {
            boolean checkReadme = parentEntry.getDescription().length() == 0;
            checkReadme = false;
            long t3 = System.currentTimeMillis();
            FTPFile[] files = ftpClient.listFiles(path);
            long t4 = System.currentTimeMillis();
            //                System.err.println ("listFiles:" + (t4-t3));

            for (int i = 0; i < files.length; i++) {
                String name = files[i].getName().toLowerCase();
                if ((pattern != null) && !name.matches(pattern)) {
                    continue;
                }
                if (checkReadme) {
                    if (name.equals("readme") || name.equals("readme.txt")) {
                        try {
                            InputStream fis = ftpClient.retrieveFileStream(path + "/" + files[i].getName());
                            if (fis != null) {
                                String desc = HtmlUtils.entityEncode(IOUtil.readInputStream(fis));
                                parentEntry.setDescription(HtmlUtils.pre(desc));
                                fis.close();
                                ftpClient.completePendingCommand();
                            }
                        } catch (Exception exc) {
                            //                            exc.printStackTrace();
                        }
                    }
                }

                putCache(mainEntry, path + "/" + files[i].getName(), files[i]);
                ids.add(getSynthId(mainEntry, baseDir, path, files[i]));
            }
        }
    } finally {
        closeConnection(ftpClient);
    }
    long t5 = System.currentTimeMillis();

    //        System.err.println ("getSynthIds:" + (t5-t0));
    return ids;
}

From source file:org.ut.biolab.medsavant.shared.util.SeekableFTPStream.java

private int readFromStream(byte[] bytes, int offset, int len) throws IOException {

    FTPClient client = getFTPClient();
    if (position != 0) {
        client.setRestartOffset(position);
    }// w w w  .  j  a v  a  2s.  c o m
    InputStream is = client.retrieveFileStream(fileName);
    long oldPos = position;
    if (is != null) {
        int n = 0;
        while (n < len) {
            int bytesRead = is.read(bytes, offset + n, len - n);
            if (bytesRead < 0) {
                if (n == 0)
                    return -1;
                else
                    break;
            }
            n += bytesRead;
        }
        is.close();
        LOG.info(String.format("FTP read %d bytes at %d: %02x %02x %02x %02x %02x %02x %02x %02x...", len,
                oldPos, bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3],
                bytes[offset + 4], bytes[offset + 5], bytes[offset + 6], bytes[offset + 7]));
        try {
            client.completePendingCommand();
        } catch (FTPConnectionClosedException suppressed) {
        } catch (SocketTimeoutException stx) {
            // Accessing 1000 Genomes, we sometimes get a timeout for no apparent reason.
            LOG.info("Timed out during read.  Disconnecting.");
            disconnect();
        }
        position += n;
        return n;
    } else {
        String msg = String.format("Unable to retrieve input stream for file (reply code %d).",
                client.getReplyCode());
        LOG.error(msg);
        throw new IOException(msg);
    }
}

From source file:ro.kuberam.libs.java.ftclient.FTP.FTP.java

public InputStream retrieveResource(Object abstractConnection, String remoteResourcePath) throws Exception {
    long startTime = new Date().getTime();
    FTPClient connection = (FTPClient) abstractConnection;
    if (!connection.isConnected()) {
        throw new Exception(ErrorMessages.err_FTC002);
    }//from  w  w  w  . java  2s. c  o  m

    _checkResourcePath(connection, remoteResourcePath, "retrieve-resource",
            checkIsDirectory(remoteResourcePath));

    InputStream is = connection.retrieveFileStream(remoteResourcePath);

    log.info("The FTP sub-module retrieved the resource '" + remoteResourcePath + "' in "
            + (new Date().getTime() - startTime) + " ms.");

    return is;
}

From source file:tufts.oki.remoteFiling.RemoteByteStore.java

/**
 *  Get the bytes in a file associated with this byte store.
 *//* w w  w .  jav a2  s  .  c  o  m*/
public byte[] getBytes() throws osid.filing.FilingException {
    // Open the file for input access.
    InputStream stream = null;
    String fn = getFullName();

    //  Allocate a buffer to hold the file.  Note that this must fit in memory and be
    //  small in size than Integer.MAX_VALUE.
    long trueLen = length();
    if (trueLen > (long) Integer.MAX_VALUE)
        throw new osid.filing.FilingException("File is too big to read.");
    int len = (int) trueLen;
    byte[] buf = new byte[len];

    //System.out.println ("getBytes - file name to retrieve: " + fn);
    try {
        FTPClient client = rc.getClient();
        stream = client.retrieveFileStream(fn);
    } catch (java.io.IOException ex1) {
        throw new osid.filing.FilingException(osid.filing.FilingException.IO_ERROR);
    }

    //  Copy the file stream into a buffer.
    try {
        for (int i = 0; i < len; i++) {
            buf[i] = (byte) stream.read();
        }
        stream.close();
    } catch (java.io.IOException ex) {
        throw new osid.filing.FilingException(osid.filing.FilingException.IO_ERROR);
    }

    return buf;
}