Example usage for org.apache.commons.net.ftp FTPReply isPositiveCompletion

List of usage examples for org.apache.commons.net.ftp FTPReply isPositiveCompletion

Introduction

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

Prototype

public static boolean isPositiveCompletion(int reply) 

Source Link

Document

Determine if a reply code is a positive completion response.

Usage

From source file:com.gdbocom.util.SendFileinFTP.java

/**
 * FTP?/* www .j  av a2s . c om*/
 * @param server ?IP
 * @param username ??
 * @param password ?
 * @return ??
 */
public boolean connect(String server, String username, String password) {

    //FTP?
    try {
        int reply;
        ftp.connect(server);
        gzLog.Write("Connected to " + server + ".");

        // ???
        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            gzLog.Write("FTP server refused connection.");
            return false;
        }
    } catch (IOException e) {//IO?,
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                return false;
            }
        }
        gzLog.Write("Could not connect to server.");
        e.printStackTrace();
        return false;
    }

    //???
    try {
        if (!ftp.login(username, password)) {//??
            ftp.logout();
            return false;
        }
        gzLog.Write("Remote system is " + ftp.getSystemName());

    } catch (IOException e) {
        gzLog.Write(e.getMessage());
        return false;
    }
    return true;
}

From source file:com.dp2345.plugin.ftp.FtpPlugin.java

@Override
public void upload(String path, File file, String contentType) {
    PluginConfig pluginConfig = getPluginConfig();
    if (pluginConfig != null) {
        String host = pluginConfig.getAttribute("host");
        Integer port = Integer.valueOf(pluginConfig.getAttribute("port"));
        String username = pluginConfig.getAttribute("username");
        String password = pluginConfig.getAttribute("password");
        FTPClient ftpClient = new FTPClient();
        InputStream inputStream = null;
        try {//  ww w .  j  av  a2  s  .  c o  m
            inputStream = new FileInputStream(file);
            ftpClient.connect(host, port);
            ftpClient.login(username, password);
            ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                String directory = StringUtils.substringBeforeLast(path, "/");
                String filename = StringUtils.substringAfterLast(path, "/");
                if (!ftpClient.changeWorkingDirectory(directory)) {
                    String[] paths = StringUtils.split(directory, "/");
                    String p = "/";
                    ftpClient.changeWorkingDirectory(p);
                    for (String s : paths) {
                        p += s + "/";
                        if (!ftpClient.changeWorkingDirectory(p)) {
                            ftpClient.makeDirectory(s);
                            ftpClient.changeWorkingDirectory(p);
                        }
                    }
                }
                ftpClient.storeFile(filename, inputStream);
                ftpClient.logout();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(inputStream);
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                }
            }
        }
    }
}

From source file:ftp.FTPtask.java

/**
 * Change directory//from   w w  w  .j  a  va2  s .com
 * @param FTPADDR, ?  ?
  * @param user,   
  * @param password,   
  * @param ChangeFolder, ,     ,  /upload
 */
public static void FTPChangeDir(String FTPADDR, String user, String password, String ChangeFolder) {

    FTPClient client = new FTPClient();

    try {

        client.connect(FTPADDR);
        client.login(user, password);

        int replyCode = client.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode)) {
            System.out.println("Connect failed");
            return;
        }

        boolean success = client.login(user, password);
        showServerReply(client);

        if (!success) {
            System.out.println("Could not login to the server");
            return;
        }

        // Changes working directory
        success = client.changeWorkingDirectory(ChangeFolder);
        showServerReply(client);

        if (success) {
            System.out.println("Successfully changed working directory.");
        } else {
            System.out.println("Failed to change working directory. See server's reply.");
        }

        // logs out
        client.logout();
        client.disconnect();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

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

/**
 * Connects to FTP server./*from w  ww . jav a  2  s . com*/
 *
 * @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:FileHandler.JakartaFtpWrapper.java

public boolean simplyConnect(String host)
        throws IOException, UnknownHostException, FTPConnectionClosedException {
    boolean success = false;
    connect(host);//from  w  w  w.ja  v  a  2 s  . c  o m
    int reply = getReplyCode();
    if (FTPReply.isPositiveCompletion(reply))
        success = true;
    if (!success)
        disconnect();
    return success;
}

From source file:com.cyberway.issue.net.ClientFTP.java

/**
 * Connects to the FTP server at the given host and port.
 * /*from w  ww .  j a  v a  2  s  .c  om*/
 * @param host    the host of the FTP server to connect to
 * @param port    the port the FTP server listens on
 * @throws IOException  if the connection cannot be made due to IO error
 * @throws FTPException  if the server refuses the connection
 */
public void connectStrict(String host, int port) throws IOException {
    this.connect(host, port);
    int reply = this.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        throw new FTPException(reply);
    }
}

From source file:de.cismet.cids.custom.utils.formsolutions.FormSolutionFtpClient.java

/**
 * DOCUMENT ME!/*from   w  w w .j ava2  s  . c om*/
 *
 * @return  DOCUMENT ME!
 *
 * @throws  Exception  DOCUMENT ME!
 */
private FTPClient getConnectedFTPClient() throws Exception {
    final FTPClient ftpClient = new FTPClient();
    ftpClient.connect(FormSolutionsProperties.getInstance().getFtpHost());

    final int reply = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftpClient.disconnect();
        throw new Exception("Exception in connecting to FTP Server");
    }
    ftpClient.login(FormSolutionsProperties.getInstance().getFtpLogin(),
            FormSolutionsProperties.getInstance().getFtpPass());
    return ftpClient;
}

From source file:cn.zhuqi.mavenssh.web.util.test.FtpTest.java

/**
 * Description: ?FTP?//from   w w  w .  j  a  v  a2 s . c  o  m
 *
 * @param url FTP?hostname
 * @param port FTP???-1
 * @param username FTP?
 * @param password FTP?
 * @param path FTP??
 * @param filename FTP???
 * @param input ?
 * @return ?true?false
 */
public static boolean uploadFile(String url, int port, String username, String password, String path,
        String filename, InputStream input) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        // FTP?
        if (port > -1) {
            ftp.connect(url, port);
        } else {
            ftp.connect(url);
        }
        // FTP
        ftp.login(username, password);
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        ftp.changeWorkingDirectory(path);
        ftp.storeFile(filename, input);

        input.close();
        ftp.logout();
        success = true;
    } catch (IOException e) {
        success = false;
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException e) {
            }
        }
    }
    return success;
}

From source file:com.jmeter.alfresco.utils.FtpUtils.java

/**
 * Upload directory or file.//from  w  ww  .  j  a v a2  s  .  c om
 *
 * @param host the host
 * @param port the port
 * @param userName the user name
 * @param password the password
 * @param fromLocalDirOrFile the local dir
 * @param toRemoteDirOrFile the remote dir
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 */
public String uploadDirectoryOrFile(final String host, final int port, final String userName,
        final String password, final String fromLocalDirOrFile, final String toRemoteDirOrFile)
        throws IOException {

    final FTPClient ftpClient = new FTPClient();
    String responseMessage = Constants.EMPTY;
    try {
        // Connect and login to get the session
        ftpClient.connect(host, port);
        final int replyCode = ftpClient.getReplyCode();

        if (FTPReply.isPositiveCompletion(replyCode)) {
            final boolean loginSuccess = ftpClient.login(userName, password);
            if (loginSuccess) {
                LOG.debug("Connected to remote host!");
                //Use local passive mode to pass fire-wall
                //In this mode a data connection is made by opening a port on the server for the client to connect 
                //and this is not blocked by fire-wall.
                ftpClient.enterLocalPassiveMode();
                final File localDirOrFileObj = new File(fromLocalDirOrFile);
                if (localDirOrFileObj.isFile()) {
                    LOG.debug("Uploading file: " + fromLocalDirOrFile);
                    uploadFile(ftpClient, fromLocalDirOrFile,
                            toRemoteDirOrFile + FILE_SEPERATOR_LINUX + localDirOrFileObj.getName());
                } else {
                    uploadDirectory(ftpClient, toRemoteDirOrFile, fromLocalDirOrFile, EMPTY);
                }
                responseMessage = "Upload completed successfully!";
            } else {
                responseMessage = "Could not login to the remote host!";
            }
            //Log out and disconnect from the server once FTP operation is completed.
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.logout();
                } catch (IOException ignored) {
                    LOG.error("Ignoring the exception while logging out from remote host: ", ignored);
                }
                try {
                    ftpClient.disconnect();
                    LOG.debug("Disconnected from remote host!");
                } catch (IOException ignored) {
                    LOG.error("Ignoring the exception while disconnecting from remote host: ", ignored);
                }
            }
        } else {
            responseMessage = "Host connection failed!";
        }
        LOG.debug("ResponseMessage:=> " + responseMessage);
    } catch (IOException ioexcp) {
        LOG.error("IOException occured in uploadDirectoryOrFile(..): ", ioexcp);
        throw ioexcp;
    }
    return responseMessage;
}

From source file:com.japp.FtpConfigTest.java

@Before
public void setUp() throws Exception {

    folder = new File(localOutputDirectory);

    ftp = new FTPClient();
    ftp.connect(serverAddress, 3333);//from  w  ww .j  ava2 s.c o  m

    //login to server
    if (!ftp.login(userId, password)) {
        ftp.logout();
    }

    int reply = ftp.getReplyCode();
    //FTPReply stores a set of constants for FTP reply codes.
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
    }

    //enter passive mode
    ftp.enterLocalPassiveMode();
    //get system name
    LOGGER.info("Remote system is " + ftp.getSystemType());
    ftp.changeWorkingDirectory(remoteDirectory);
    LOGGER.info("Current directory is " + ftp.printWorkingDirectory());

}