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

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

Introduction

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

Prototype

public int getReplyCode() 

Source Link

Document

Returns the integer value of the reply code of the last FTP reply.

Usage

From source file:net.paissad.jcamstream.utils.FTPUtils.java

/**
 * Verifies whether or not the last previous FTP action/command finished
 * successfully, if/*from   w  w w.jav a  2s.c o  m*/
 * not then display the specified error message and throw a
 * {@link FTPException}
 * 
 * @param errorMessage
 *            - The error message to show if ever the previous ftp command
 *            did not return a correct/positive reply code.
 * @throws FTPException
 */
private void verifyReplyCode(String errorMessage) throws FTPException {
    FTPClient client = this.getFtpClient();
    if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
        System.err.println(errorMessage);
        throw new FTPException(errorMessage);
    }
}

From source file:ftp_server.FileUpload.java

public void uploading() {
    FTPClient client = new FTPClient();
    FileInputStream fis = null;/*  w w w.  j a  v a  2  s  . c  o m*/
    try {
        client.connect("shamalmahesh.net78.net");
        client.login("a9959679", "9P3IckDo");
        client.setFileType(FTP.BINARY_FILE_TYPE);
        int reply = client.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            System.out.println("Uploading....");
        } else {
            System.out.println("Failed connect to the server!");
        }
        //            String filename = "DownloadedLook1.txt";
        String path = "D:\\jpg\\";
        //            String filename = "1.jpg";
        String filename = "appearance.jpg";
        System.out.println(path + filename);
        fis = new FileInputStream(path + filename);
        System.out.println(fis.toString());
        boolean status = client.storeFile("/public_html/testing/" + filename, fis);
        System.out.println("Status : " + status);
        System.out.println("Done!");
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:edu.mda.bioinfo.ids.DownloadFiles.java

private void downloadFromFtpToFile(String theServer, String theServerFile, String theLocalFile)
        throws IOException, Exception {
    FTPClient ftp = new FTPClient();
    try {/*w  ww .j  av a2 s . co  m*/
        int reply = 0;
        boolean replyB = false;
        ftp.connect(theServer);
        System.out.print(ftp.getReplyString());
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            System.err.println("FTP server refused connection.");
        } else {
            ftp.login("anonymous", "anonymous");
            replyB = ftp.setFileType(FTP.BINARY_FILE_TYPE);
            System.out.print(ftp.getReplyString());
            System.out.println("replyB= " + replyB);
            if (false == replyB) {
                throw new Exception("Unable to login to " + theServer);
            }
            OutputStream output = new FileOutputStream(theLocalFile);
            replyB = ftp.retrieveFile(theServerFile, output);
            System.out.print(ftp.getReplyString());
            System.out.println("replyB= " + replyB);
            if (false == replyB) {
                throw new Exception("Unable to retrieve " + theServerFile);
            }
        }
        ftp.logout();
    } catch (IOException rethrownExp) {
        System.err.println("exception " + rethrownExp.getMessage());
        throw rethrownExp;
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ignore) {
                // do nothing
            }
        }
    }
}

From source file:net.paissad.jcamstream.utils.FTPUtils.java

/**
 * Logout and then disconnect from the FTP server.
 * //ww  w . j a v a 2 s .co m
 * @throws IOException
 * 
 * @see org.apache.commons.net.ftp.FTPClient#logout()
 * @see org.apache.commons.net.ftp.FTPClient#disconnect()
 */
public void logoutAndDisconnect() throws IOException {
    FTPClient client = this.getFtpClient();

    if (client.isConnected() && !FTPReply.isPositiveIntermediate(client.getReplyCode())) {
        client.logout();
        client.disconnect();
        System.out.println("Disconnected successfully from FTP server.");
    }

    if (client.isConnected() && !client.completePendingCommand()) {
        System.err.println("Something failed !");
    }
}

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

public void uploadMutiFileWithFTP(String targetDirectory, FileTransferController fileTransferController) {
    try {/* w w  w  .j  a v a 2 s.c  o  m*/
        FtpProperties properties = new ResourcesProperties().loadFTPProperties();

        FTPClient ftp = new FTPClient();
        ftp.connect(properties.getFtp_server());
        if (!ftp.login(properties.getFtp_username(), properties.getFtp_password())) {
            ftp.logout();
        }
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
        }
        ftp.enterLocalPassiveMode();
        System.out.println("Remote system is " + ftp.getSystemName());
        ftp.changeWorkingDirectory(targetDirectory);
        System.out.println("Current directory is " + ftp.printWorkingDirectory());
        FTPFile[] ftpFiles = ftp.listFiles();
        if (ftpFiles != null && ftpFiles.length > 0) {
            for (FTPFile file : ftpFiles) {
                if (!file.isFile()) {
                    continue;
                }
                System.out.println("File is " + file.getName());
                OutputStream output;
                output = new FileOutputStream(
                        properties.getFtp_remote_directory() + File.separator + file.getName());
                ftp.retrieveFile(file.getName(), output);
                output.close();
            }
        }
        ftp.logout();
        ftp.disconnect();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.clican.pluto.cms.core.service.impl.SiteServiceImpl.java

public FTPClient getFTPClient(ISite site) throws IOException {
    String url = site.getUrl();/*from ww w.j  av  a  2s .co m*/
    if (url == null) {
        return null;
    }
    if (url.startsWith("ftp://")) {
        int port = 21;
        String hostname = null;
        try {
            url = url.substring(6);
            if (url.indexOf(":") != -1) {
                hostname = url.substring(0, url.indexOf(":"));
                if (url.endsWith("/")) {
                    port = Integer.parseInt(url.substring(url.indexOf(":") + 1, url.length() - 1));
                } else {
                    port = Integer.parseInt(url.substring(url.indexOf(":") + 1));
                }
            } else {
                if (url.endsWith("/")) {
                    hostname = url.substring(0, url.length() - 1);
                } else {
                    hostname = url;
                }
            }
        } catch (Exception e) {
            throw new UnknownHostException(url);
        }
        FTPClient client = new FTPClient();
        client.connect(hostname, port);
        if (log.isDebugEnabled()) {
            log.debug("Connected to " + url + ".");
            log.debug(client.getReplyString());
        }
        int reply = client.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            client.disconnect();
            log.warn("FTP server " + url + " refused connection.");
            return null;
        }
        if (StringUtils.isNotEmpty(site.getUsername())) {
            boolean login = client.login(site.getUsername(), site.getPassword());
            if (!login) {
                client.disconnect();
                return null;
            }
        }

        return client;
    }
    return null;
}

From source file:com.unicomer.opos.inhouse.gface.ejb.impl.GfaceGuatefacturasControlFtpEjbLocalImpl.java

public FTPClient getFtpClient(HashMap<String, String> params) {
    String urlServer = params.get("urlServer");
    String usuario = params.get("user");
    String pass = params.get("pass");
    //        String workingDir = params.get("workingDir");

    FTPClient ret = null;//from w ww  .  j  a v  a  2 s  .co  m
    try {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(InetAddress.getByName(urlServer));
        ftpClient.login(usuario, pass);
        //Verificar conexin con el servidor.
        int reply = ftpClient.getReplyCode();
        System.out.println("Estatus de conexion FTP:" + reply);
        if (FTPReply.isPositiveCompletion(reply)) {
            System.out.println("Conectado exitosamente");
        } else {
            System.out.println("No se pudo conectar con el servidor");
        }
        //directorio de trabajo
        //            ftpClient.changeWorkingDirectory(workingDir);
        System.out.println("Establecer carpeta de trabajo");
        //Activar que se envie cualquier tipo de archivo
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();
        ret = ftpClient;
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
    return ret;
}

From source file:ftp_server.FileUpload.java

public void uploadingWithProgress() {
    FTPClient ftp = new FTPClient();
    FileInputStream fis = null;/* ww  w .ja v a 2  s .co  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.eryansky.common.utils.ftp.FtpFactory.java

/**
 * ?FTP?./*from w  w w. j a va  2 s  .  c  o  m*/
 * 
 * @param path
 *            FTP??
 * @param filename
 *            FTP???
 * @param input
 *            ?
 * @return ?true?false
 */
public boolean ftpUploadFile(String path, String filename, InputStream input) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    ftp.setControlEncoding("UTF-8");
    try {
        int reply;
        ftp.connect(url, port);
        ftp.login(username, password);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        // 
        ftp.changeWorkingDirectory(path);
        ftp.setBufferSize(1024);
        ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
        // 
        ftp.storeFile(filename, input);
        input.close();
        ftp.logout();
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return success;
}

From source file:jenkins.plugins.publish_over_ftp.BapFtpHostConfiguration.java

private void connect(final BapFtpClient client) throws IOException {
    final FTPClient ftpClient = client.getFtpClient();
    ftpClient.connect(getHostnameTrimmed(), getPort());
    final int responseCode = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(responseCode)) {
        exception(client, Messages.exception_connectFailed(getHostnameTrimmed(), getPort(), responseCode));
    }//from  w  ww.  jav a2  s  . c o m
    setDataTransferMode(ftpClient);
}