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:uk.ac.bbsrc.tgac.miso.core.util.TransmissionUtils.java

public static FTPClient ftpConnect(String host, int port, String username, String password) throws IOException {
    FTPClient ftp = new FTPClient();
    try {/*from  w  w w . jav  a2 s .  c  om*/
        ftp.connect(host, port);
        log.debug("Trying " + host + ":" + port);
        log.debug(ftp.getReplyString());
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new IOException("FTP server refused connection: " + reply);
        } else {
            log.debug("Connected");
        }

        ftp.login(username, password);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
    } catch (NoRouteToHostException e) {
        throw new IOException("Couldn't connect to printer: " + e.getMessage(), e);
    } catch (UnknownHostException e) {
        throw new IOException("Couldn't connect to printer: " + e.getMessage(), e);
    }
    return ftp;
}

From source file:uk.ac.bbsrc.tgac.miso.core.util.TransmissionUtils.java

public static boolean ftpPut(FTPClient ftp, String path, List<File> files, boolean autoLogout,
        boolean autoMkdir) throws IOException {
    boolean error = false;
    FileInputStream fis = null;//from w  w w.ja v a2  s.  c  om

    try {
        if (ftp == null || !ftp.isConnected()) {
            error = true;
            throw new IOException(
                    "FTP client isn't connected. Please supply a client that has connected to the host.");
        }

        if (path != null) {
            if (autoMkdir) {
                if (!ftp.makeDirectory(path)) {
                    error = true;
                    throw new IOException("Cannot create desired path on the server.");
                }
            }

            if (!ftp.changeWorkingDirectory(path)) {
                error = true;
                throw new IOException("Desired path does not exist on the server");
            }
        }

        log.info("All OK - transmitting " + files.size() + " file(s)");

        for (File f : files) {
            fis = new FileInputStream(f);
            if (!ftp.storeFile(f.getName(), fis)) {
                error = true;
                log.error("Error storing file: " + f.getName());
            }

            boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode());
            if (!success) {
                error = true;
                log.error("Error storing file: " + f.getName() + " (" + success + ")");
            }
        }

        if (autoLogout) {
            ftp.logout();
        }
    } catch (IOException e) {
        error = true;
        log.error("ftp", e);
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }

            if (autoLogout) {
                if (ftp != null && ftp.isConnected()) {
                    ftp.disconnect();
                }
            }
        } catch (IOException ioe) {
            log.error("ftp", ioe);
        }
    }

    // return inverse error boolean, just to make downstream conditionals easier
    return !error;
}

From source file:uk.ac.manchester.cs.datadesc.validator.utils.UrlReader.java

private FTPClient connect() throws VoidValidatorException {
    FTPClient ftp = new FTPClient();

    //CB consider replacing with logger
    // suppress login details
    ftp.addProtocolCommandListener(new FtpListener(logger));

    try {//from  www .  ja va2  s.co  m
        int reply;
        if (uri.getPort() > 0) {
            ftp.connect(uri.getHost(), uri.getPort());
        } else {
            ftp.connect(uri.getHost());
        }
        logger.info("Connected to " + uri.getHost() + " on port "
                + (uri.getPort() > 0 ? uri.getPort() : ftp.getDefaultPort()));

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

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new VoidValidatorException("Unable to connect to FTP server " + uri);
        }
        return ftp;
    } catch (IOException ex) {
        disconnect(ftp);
        throw new VoidValidatorException("Error to connect to FTP server " + uri, ex);
    }
}

From source file:uk.sipperfly.utils.FTPUtil.java

public static void reconnect() throws SocketException, IOException {
    FTPClient ftpClient = new FTPClient();
    ftpClient.setControlEncoding("UTF-8");
    ftpClient.connect(host, port);//w ww .jav  a  2s . c o m
    ftpClient.login(username, password);
    if (mode.equalsIgnoreCase("passive")) {
        ftpClient.enterLocalPassiveMode();
    } else if (mode.equalsIgnoreCase("active")) {
        ftpClient.enterLocalActiveMode();
    }
    int reply = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        Logger.getLogger(GACOM).log(Level.INFO, "FTP Login: ".concat(ftpClient.getReplyString()));
        ftpClient.disconnect();
    }
    ftpClient.setKeepAlive(true);
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
    ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
    ftpClient.setControlKeepAliveTimeout(300);
    //      ftpClient.sendSiteCommand("RECFM=FB");
    //      ftpClient.sendSiteCommand("LRECL=2000");
    //      ftpClient.sendSiteCommand("BLKSIZE=27000");
    //      ftpClient.sendSiteCommand("CY");
    //      ftpClient.sendSiteCommand("PRI= 50");
    //      ftpClient.sendSiteCommand("SEC=25");

    //      ftpClient.sendSiteCommand("RECFM=FB");
    //      ftpClient.sendSiteCommand("LRECL=2000");
    //      ftpClient.sendSiteCommand("BLOCKSIZE=27000");
    //      ftpClient.sendSiteCommand("SPACE=(CYL,(30,300),RLSE)"); 
    //      ftpClient.sendSiteCommand("TR");
    //      ftpClient.sendSiteCommand("PRI=450");
    //      ftpClient.sendSiteCommand("SEC=4500");

    Logger.getLogger(GACOM).log(Level.INFO, "Reconnected FTP");
    System.out.println("reconnected");
}

From source file:websync2.SyncDesign.java

public void copyFileFTP(File temp, File servF) {

    InputStream inputStream = null;
    try {//  www . j  av  a  2 s. c  o  m
        FTPClient ftpclient = ftp.getFtpClient();
        ftpclient.changeToParentDirectory();
        String firstRemoteFile = servF.getAbsolutePath();
        String[] pathA = firstRemoteFile.split("/");
        if (pathA != null) {
            for (int i = 0; i < pathA.length - 1; i++) {
                if ("".equals(pathA[i])) {
                    continue;
                }
                InputStream is = ftpclient.retrieveFileStream(pathA[i]);
                int retCode = ftpclient.getReplyCode();
                if (retCode == 550) {
                    ftpclient.makeDirectory(pathA[i]);
                }
                ftpclient.changeWorkingDirectory(pathA[i]);
            }
            inputStream = new FileInputStream(temp);
            boolean done = ftpclient.storeFile(pathA[pathA.length - 1], inputStream);
            if (done) {
                System.out.println("The first file is uploaded successfully.");
            }
        }

    } catch (FileNotFoundException ex) {
        Logger.getLogger(SyncDesign.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(SyncDesign.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            inputStream.close();
        } catch (IOException ex) {
            Logger.getLogger(SyncDesign.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}