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

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

Introduction

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

Prototype

public void setRestartOffset(long offset) 

Source Link

Document

Sets the restart offset for file transfers.

Usage

From source file:com.feilong.tools.net.ZClientTest.java

/**
 * Gets the files.//  ww  w . j av  a  2  s. c o m
 * 
 * @param ftp
 *            the ftp
 * @param localDir
 *            the local dir
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
private static void testGetFiles(FTPClient ftp, File localDir) throws IOException {
    String[] names = ftp.listNames();
    for (String name : names) {
        File file = new File(localDir.getPath() + File.separator + name);
        if (!file.exists()) {
            file.createNewFile();
        }
        long pos = file.length();
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        raf.seek(pos);
        ftp.setRestartOffset(pos);
        InputStream is = ftp.retrieveFileStream(name);
        if (is == null) {
            log.info("no such file:" + name);
        } else {
            log.info("start getting file:" + name);
            int b;
            while ((b = is.read()) != -1) {
                raf.write(b);
            }
            is.close();
            if (ftp.completePendingCommand()) {
                log.info("done!");
            } else {
                log.info("can't get file:" + name);
            }
        }
        raf.close();
    }
}

From source file:com.datos.vfs.provider.ftp.FTPClientWrapper.java

@Override
public InputStream retrieveFileStream(final String relPath, final long restartOffset) throws IOException {
    try {//from   w ww.j a va2  s  . com
        final FTPClient client = getFtpClient();
        client.setRestartOffset(restartOffset);
        return client.retrieveFileStream(relPath);
    } catch (final IOException e) {
        disconnect();
        final FTPClient client = getFtpClient();
        client.setRestartOffset(restartOffset);
        return client.retrieveFileStream(relPath);
    }
}

From source file:GridFDock.DataDistribute.java

public Status uploadFile(String remoteFile, File localFile, FTPClient ftpClient, long remoteSize)
        throws IOException {

    long step = localFile.length() / 100;
    long process = 0;
    long localreadbytes = 0L;
    boolean tmp2 = true;
    Status result = null;//from w  w w.j  av a 2s  .  c om
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

    while (tmp2) {
        RandomAccessFile raf = new RandomAccessFile(localFile, "r");
        OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes(CODING_1), CODING_2));

        if (remoteSize > 0) {
            ftpClient.setRestartOffset(remoteSize);
            process = remoteSize / step;
            raf.seek(remoteSize);
            localreadbytes = remoteSize;
        }
        byte[] bytes = new byte[1024];
        int c;
        while ((c = raf.read(bytes)) != -1) {
            out.write(bytes, 0, c);
            localreadbytes += c;
            if (localreadbytes / step != process) {
                process = localreadbytes / step;
                // System.out.println("Upload Progress" + process);

            }
        }
        out.flush();
        raf.close();
        out.close();
        boolean judge = ftpClient.completePendingCommand();

        if (judge) {
            result = Status.Upload_From_Break_Success;
            tmp2 = false;
        } else {
            result = Status.Upload_New_File_Failed;
        }
    }
    return result;
}

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  v a  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.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 av a 2s .c  om*/
    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);
    }
}