Example usage for org.apache.commons.net.ftp FTP BINARY_FILE_TYPE

List of usage examples for org.apache.commons.net.ftp FTP BINARY_FILE_TYPE

Introduction

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

Prototype

int BINARY_FILE_TYPE

To view the source code for org.apache.commons.net.ftp FTP BINARY_FILE_TYPE.

Click Source Link

Document

A constant used to indicate the file(s) being transfered should be treated as a binary image, i.e., no translations should be performed.

Usage

From source file:com.claim.support.FtpUtil.java

public void uploadSingleFileWithFTP(String direcPath, FileTransferController fileTransferController) {
    FTPClient ftpClient = new FTPClient();
    InputStream inputStream = null;
    try {//from  w ww.  j a  va 2 s. com
        FtpProperties properties = new ResourcesProperties().loadFTPProperties();
        ftpClient.connect(properties.getFtp_server(), properties.getFtp_port());
        ftpClient.login(properties.getFtp_username(), properties.getFtp_password());
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        File secondLocalFile = new File(direcPath);
        String secondRemoteFile = "WorkshopDay2.docx";
        inputStream = new FileInputStream(secondLocalFile);
        System.out.println("Start uploading second file");
        OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
        byte[] bytesIn = new byte[4096];
        int read = 0;
        while ((read = inputStream.read(bytesIn)) != -1) {
            outputStream.write(bytesIn, 0, read);
        }
        inputStream.close();
        outputStream.close();
        boolean completed = ftpClient.completePendingCommand();
        if (completed) {
            System.out.println("The second file is uploaded successfully.");
        }
    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:m09_uf3_7.ClientFTP.java

public void activarEnvio() throws IOException {
    //--CODE--
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
}

From source file:au.org.intersect.dms.wn.transports.impl.FtpConnection.java

public FtpConnection(String server, String username, String password) {
    LOGGER.debug("ftp connect to server:{} using username:{} and password:{}",
            new String[] { server, username, password });
    boolean ok = false;
    params = new ConnectionParams("ftp", server, username, null);
    try {//from   ww w. j  a  v  a 2 s  .  c  o m
        client = new FTPClient();
        client.connect(server);
        client.enterLocalPassiveMode();
        if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
            throw new TransportError();
        }
        if (!client.login(username, password)) {
            throw new NotAuthorizedError();
        }
        client.setFileType(FTP.BINARY_FILE_TYPE);
        ok = true;
    } catch (SocketException e) {
        throw new TransportError(e);
    } catch (IOException e) {
        throw new TransportError(e);
    } finally {
        if (!ok && client != null) {
            try {
                client.disconnect();
            } catch (IOException e) {
                // swallows exception
            }
        }
    }
}

From source file:ftp_server.FileUpload.java

public void uploadingWithProgress() {
    FTPClient ftp = new FTPClient();
    FileInputStream fis = null;/*from w w  w  .j a  va  2 s .  c  o  m*/
    try {
        ftp.connect("shamalmahesh.net78.net");
        ftp.login("a9959679", "9P3IckDo");
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.changeWorkingDirectory("/public_html/testing");
        int reply = ftp.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            System.out.println("Uploading....");
        } else {
            System.out.println("Failed connect to the server!");
        }
        File f1 = new File("D:\\jpg\\1.jpg");

        //            fis = new FileInputStream(ftp.storeFile("one.jpg", fis));
        System.out.println("Done!");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.zurich.lifeac.intra.control.FTPUtil.java

public static boolean downloadSingleFile(FTPClient ftpClient, String remoteFilePath, String savePath)
        throws IOException {

    // code to download a file...
    File downloadFile = new File(savePath);

    File parentDir = downloadFile.getParentFile();
    if (!parentDir.exists()) {
        parentDir.mkdir();//from www .j  ava  2s .c om
    }

    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
    try {
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        System.out.println("downloadSingle FileremoteFilePath:" + remoteFilePath);
        return ftpClient.retrieveFile(remoteFilePath, outputStream);
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

From source file:com.microsoft.azuretools.utils.WebAppUtils.java

public static FTPClient getFtpConnection(PublishingProfile pp) throws IOException {

    FTPClient ftp = new FTPClient();

    System.out.println("\t\t" + pp.ftpUrl());
    System.out.println("\t\t" + pp.ftpUsername());
    System.out.println("\t\t" + pp.ftpPassword());

    URI uri = URI.create("ftp://" + pp.ftpUrl());
    ftp.connect(uri.getHost(), 21);//ww w .  j a  v a 2  s.c o  m
    final int replyCode = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(replyCode)) {
        ftp.disconnect();
        throw new ConnectException("Unable to connect to FTP server");
    }

    if (!ftp.login(pp.ftpUsername(), pp.ftpPassword())) {
        throw new ConnectException("Unable to login to FTP server");
    }

    ftp.setControlKeepAliveTimeout(Constants.connection_read_timeout_ms);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    ftp.enterLocalPassiveMode();//Switch to passive mode

    return ftp;
}

From source file:mysynopsis.FTPUploader.java

public static String uploadWizard() throws IOException {

    FTPClient connect;/*from www . j a v a  2 s .  co  m*/
    connect = null;

    try {
        connect = new FTPClient();
        connect.connect(server, 21);
        int reply = connect.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            connect.disconnect();
            return "Can't Connect to the Server";
        }

        boolean check = connect.login(user, pass);
        if (!check) {
            return "Username or password Incorrect";
        }

        connect.setFileType(FTP.BINARY_FILE_TYPE);
        connect.enterLocalPassiveMode();

        InputStream input;
        try {
            input = new FileInputStream(new File("index.html"));
            connect.storeFile(dir + "index.html", input);
            input.close();
            connect.logout();
            connect.disconnect();

        } catch (IOException ex) {
            return "You need to put a slash (/) at the end";
        }
    } catch (IOException iOException) {
        return "Wrong Server Information. Please Try again";
    }
    return "File Transfer Successful!";
}

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

/**
 * Connects to FTP server.//  w  w w  .  j  a  v a 2s . co  m
 *
 * @param host FTP server address or name
 * @param port FTP server port
 * @param user user name
 * @param password user password
 * @param isTextMode text / binary mode switch
 * @throws IOException on I/O errors
 */
public void connect(String host, int port, String user, String password, boolean isTextMode)
        throws IOException {
    // Connect to server.
    try {
        ftp.connect(host, port);
    } catch (UnknownHostException ex) {
        throw new IOException("Can't find FTP server '" + host + "'");
    }

    // Check rsponse after connection attempt.
    int reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        disconnect();
        throw new IOException("Can't connect to server '" + host + "'");
    }

    if (user == "") {
        user = ANONYMOUS_LOGIN;
    }

    // Login.
    if (!ftp.login(user, password)) {
        is_connected = false;
        disconnect();
        throw new IOException("Can't login to server '" + host + "'");
    } else {
        is_connected = true;
    }

    // Set data transfer mode.
    if (isTextMode) {
        ftp.setFileType(FTP.ASCII_FILE_TYPE);
    } else {
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
    }
}

From source file:GridFDock.DataDistribute.java

public Status download(String remote, String local) throws IOException {
    // The mode of transfer
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    ftpClient.enterLocalPassiveMode();//from w ww  .  j  a v a2s .co  m
    // Set the binary for transferring
    // ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    Status result = null;
    boolean tmp1 = true;
    // Check the file
    FTPFile[] files = ftpClient.listFiles(new String(remote.getBytes(CODING_1), CODING_2));
    if (files.length != 1) {
        System.out.println("The remote file does not exist!");
        return Status.Remote_File_Noexist;
    }
    int i = 0;
    while (tmp1) {
        long lRemoteSize = files[0].getSize();

        File f = new File(local);
        // Judge whether the file exist.

        if (f.exists()) {
            long localSize = f.length();

            if (localSize >= lRemoteSize) {
                System.out.println(
                        "The size of local file is lager than the size of remote's file, and stop it.");
                return Status.Local_Bigger_Remote;
            }

            // record the downloading status.
            FileOutputStream out = new FileOutputStream(f, true);
            ftpClient.setRestartOffset(localSize);
            InputStream in = ftpClient.retrieveFileStream(new String(remote.getBytes(CODING_1), CODING_2));
            byte[] bytes = new byte[1024];
            long step = lRemoteSize / 100;
            long process = localSize / step;
            int c;
            while ((c = in.read(bytes)) != -1) {
                out.write(bytes, 0, c);
                localSize += c;
                long nowProcess = localSize / step;
                if (nowProcess > process) {
                    process = nowProcess;
                    /*
                     * if (process % 10 == 0) System.out.println(
                     * "Download Progressing" + process);
                     */

                }
            }
            in.close();
            out.close();
            boolean isDo = ftpClient.completePendingCommand();
            if (isDo && localSize == lRemoteSize) {
                result = Status.Download_From_Break_Success;
                tmp1 = false;
            } else {
                if (i > 10) {
                    tmp1 = false;
                }
                i++;
                result = Status.Download_From_Break_Failed;
            }
        } else {
            OutputStream out = new FileOutputStream(f);
            InputStream in = ftpClient.retrieveFileStream(new String(remote.getBytes(CODING_1), CODING_2));
            byte[] bytes = new byte[1024];
            long step = lRemoteSize / 100;
            long process = 0;
            long localSize = 0L;
            int c;
            while ((c = in.read(bytes)) != -1) {
                out.write(bytes, 0, c);
                localSize += c;
                long nowProcess = localSize / step;
                if (nowProcess > process) {
                    process = nowProcess;
                    /*
                     * if (process % 10 == 0) System.out.println(
                     * "Download Progressing" + process);
                     */
                }
            }
            in.close();
            out.close();
            boolean upNewStatus = ftpClient.completePendingCommand();

            if (upNewStatus && localSize == lRemoteSize) {
                result = Status.Download_New_Success;
                tmp1 = false;
            } else {
                if (i > 10) {
                    tmp1 = false;
                }
                i++;
                result = Status.Download_New_Failed;
            }
        }
    }
    return result;

}

From source file:hd3gtv.storage.AbstractFileBridgeFtp.java

private void reconnect() throws IOException {
    try {//from   w  w  w.ja va  2s  . com
        ftpclient.disconnect();
    } catch (Exception e) {
    }

    ftpclient.connect(configurator.host, configurator.port);

    if (ftpclient.login(configurator.username, configurator.password) == false) {
        ftpclient.logout();
        throw new IOException("Can't login to server");
    }
    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();
    }
}