Search in sources :

Example 81 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project camel by apache.

the class FtpProducerDisconnectOnBatchCompleteTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // ask the singleton FtpEndpoint to make use of a custom FTPClient
    // so that we can hold a reference on it inside the test below
    FtpEndpoint<?> endpoint = context.getEndpoint(getFtpUrl(), FtpEndpoint.class);
    endpoint.setFtpClient(new FTPClient());
}
Also used : FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 82 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project javautils by jiadongpo.

the class FTPUtilImpl method createFTPClien.

// 创建变连接FTP
private FTPClient createFTPClien(FTPVo vo) {
    FTPClient client = new FTPClient();
    int reply = -1;
    try {
        client.connect(vo.getHostName(), vo.getPort());
        client.login(vo.getUsername(), vo.getPassword());
        reply = client.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            client.disconnect();
            return null;
        } else {
            client.setControlEncoding(vo.getRemoteEncoding());
            client.setFileType(FTPClient.BINARY_FILE_TYPE);
            if (vo.isPassiveMode()) {
                client.enterLocalPassiveMode();
            } else {
                client.enterRemotePassiveMode();
            }
            client.cwd(vo.getRemoteDir());
        }
        return client;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 83 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project ofbiz-framework by apache.

the class FtpServices method putFile.

public static Map<String, Object> putFile(DispatchContext dctx, Map<String, ?> context) {
    Locale locale = (Locale) context.get("locale");
    Debug.logInfo("[putFile] starting...", module);
    InputStream localFile = null;
    try {
        localFile = new FileInputStream((String) context.get("localFilename"));
    } catch (IOException ioe) {
        Debug.logError(ioe, "[putFile] Problem opening local file", module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonFtpFileCannotBeOpen", locale));
    }
    List<String> errorList = new LinkedList<>();
    FTPClient ftp = new FTPClient();
    try {
        Integer defaultTimeout = (Integer) context.get("defaultTimeout");
        if (UtilValidate.isNotEmpty(defaultTimeout)) {
            Debug.logInfo("[putFile] set default timeout to: " + defaultTimeout.intValue() + " milliseconds", module);
            ftp.setDefaultTimeout(defaultTimeout.intValue());
        }
        Debug.logInfo("[putFile] connecting to: " + (String) context.get("hostname"), module);
        ftp.connect((String) context.get("hostname"));
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            Debug.logInfo("[putFile] Server refused connection", module);
            errorList.add(UtilProperties.getMessage(resource, "CommonFtpConnectionRefused", locale));
        } else {
            String username = (String) context.get("username");
            String password = (String) context.get("password");
            Debug.logInfo("[putFile] logging in: username=" + username + ", password=" + password, module);
            if (!ftp.login(username, password)) {
                Debug.logInfo("[putFile] login failed", module);
                errorList.add(UtilProperties.getMessage(resource, "CommonFtpLoginFailure", UtilMisc.toMap("username", username, "password", password), locale));
            } else {
                Boolean binaryTransfer = (Boolean) context.get("binaryTransfer");
                boolean binary = (binaryTransfer == null) ? false : binaryTransfer.booleanValue();
                if (binary) {
                    ftp.setFileType(FTP.BINARY_FILE_TYPE);
                }
                Boolean passiveMode = (Boolean) context.get("passiveMode");
                boolean passive = (passiveMode == null) ? true : passiveMode.booleanValue();
                if (passive) {
                    ftp.enterLocalPassiveMode();
                }
                Debug.logInfo("[putFile] storing local file remotely as: " + context.get("remoteFilename"), module);
                if (!ftp.storeFile((String) context.get("remoteFilename"), localFile)) {
                    Debug.logInfo("[putFile] store was unsuccessful", module);
                    errorList.add(UtilProperties.getMessage(resource, "CommonFtpFileNotSentSuccesfully", UtilMisc.toMap("replyString", ftp.getReplyString()), locale));
                } else {
                    Debug.logInfo("[putFile] store was successful", module);
                    List<String> siteCommands = checkList(context.get("siteCommands"), String.class);
                    if (siteCommands != null) {
                        for (String command : siteCommands) {
                            Debug.logInfo("[putFile] sending SITE command: " + command, module);
                            if (!ftp.sendSiteCommand(command)) {
                                errorList.add(UtilProperties.getMessage(resource, "CommonFtpSiteCommandFailed", UtilMisc.toMap("command", command, "replyString", ftp.getReplyString()), locale));
                            }
                        }
                    }
                }
            }
            ftp.logout();
        }
    } catch (IOException ioe) {
        Debug.logWarning(ioe, "[putFile] caught exception: " + ioe.getMessage(), module);
        errorList.add(UtilProperties.getMessage(resource, "CommonFtpProblemWithTransfer", UtilMisc.toMap("errorString", ioe.getMessage()), locale));
    } finally {
        try {
            if (ftp.isConnected()) {
                ftp.disconnect();
            }
        } catch (Exception e) {
            Debug.logWarning(e, "[putFile] Problem with FTP disconnect: ", module);
        }
        try {
            localFile.close();
        } catch (Exception e) {
            Debug.logWarning(e, "[putFile] Problem closing local file: ", module);
        }
    }
    if (errorList.size() > 0) {
        Debug.logError("[putFile] The following error(s) (" + errorList.size() + ") occurred: " + errorList, module);
        return ServiceUtil.returnError(errorList);
    }
    Debug.logInfo("[putFile] finished successfully", module);
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) LinkedList(java.util.LinkedList) FTPClient(org.apache.commons.net.ftp.FTPClient) IOException(java.io.IOException)

Example 84 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project ofbiz-framework by apache.

the class FtpServices method getFile.

public static Map<String, Object> getFile(DispatchContext dctx, Map<String, ?> context) {
    Locale locale = (Locale) context.get("locale");
    String localFilename = (String) context.get("localFilename");
    OutputStream localFile = null;
    try {
        localFile = new FileOutputStream(localFilename);
    } catch (IOException ioe) {
        Debug.logError(ioe, "[getFile] Problem opening local file", module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonFtpFileCannotBeOpen", locale));
    }
    List<String> errorList = new LinkedList<>();
    FTPClient ftp = new FTPClient();
    try {
        Integer defaultTimeout = (Integer) context.get("defaultTimeout");
        if (UtilValidate.isNotEmpty(defaultTimeout)) {
            Debug.logInfo("[getFile] Set default timeout to: " + defaultTimeout.intValue() + " milliseconds", module);
            ftp.setDefaultTimeout(defaultTimeout.intValue());
        }
        ftp.connect((String) context.get("hostname"));
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            errorList.add(UtilProperties.getMessage(resource, "CommonFtpConnectionRefused", locale));
        } else {
            String username = (String) context.get("username");
            String password = (String) context.get("password");
            if (!ftp.login(username, password)) {
                errorList.add(UtilProperties.getMessage(resource, "CommonFtpLoginFailure", UtilMisc.toMap("username", username, "password", password), locale));
            } else {
                Boolean binaryTransfer = (Boolean) context.get("binaryTransfer");
                boolean binary = (binaryTransfer == null) ? false : binaryTransfer.booleanValue();
                if (binary) {
                    ftp.setFileType(FTP.BINARY_FILE_TYPE);
                }
                Boolean passiveMode = (Boolean) context.get("passiveMode");
                boolean passive = (passiveMode == null) ? false : passiveMode.booleanValue();
                if (passive) {
                    ftp.enterLocalPassiveMode();
                }
                if (!ftp.retrieveFile((String) context.get("remoteFilename"), localFile)) {
                    errorList.add(UtilProperties.getMessage(resource, "CommonFtpFileNotSentSuccesfully", UtilMisc.toMap("replyString", ftp.getReplyString()), locale));
                }
            }
            ftp.logout();
        }
    } catch (IOException ioe) {
        Debug.logWarning(ioe, "[getFile] caught exception: " + ioe.getMessage(), module);
        errorList.add(UtilProperties.getMessage(resource, "CommonFtpProblemWithTransfer", UtilMisc.toMap("errorString", ioe.getMessage()), locale));
    } finally {
        try {
            if (ftp.isConnected()) {
                ftp.disconnect();
            }
        } catch (Exception e) {
            Debug.logWarning(e, "[getFile] Problem with FTP disconnect: ", module);
        }
        try {
            localFile.close();
        } catch (Exception e) {
            Debug.logWarning(e, "[getFile] Problem closing local file: ", module);
        }
    }
    if (errorList.size() > 0) {
        Debug.logError("[getFile] The following error(s) (" + errorList.size() + ") occurred: " + errorList, module);
        return ServiceUtil.returnError(errorList);
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) LinkedList(java.util.LinkedList) FTPClient(org.apache.commons.net.ftp.FTPClient) IOException(java.io.IOException)

Example 85 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project assembly64fx by freabemania.

the class FTPService method getNewClient.

public FTPClient getNewClient() throws Exception {
    FTPClient ftpClient = new FTPClient();
    ftpClient.setConnectTimeout(SOCKET_TIMEOUT);
    ftpClient.connect(Support.A64_HOSTNAME, Support.FTP_PORT);
    ftpClient.setSoTimeout(SOCKET_TIMEOUT);
    ftpClient.setBufferSize(1024000);
    ftpClient.login(Support.USER, Support.PASS);
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
    ftpClient.enterLocalPassiveMode();
    return ftpClient;
}
Also used : FTPClient(org.apache.commons.net.ftp.FTPClient)

Aggregations

FTPClient (org.apache.commons.net.ftp.FTPClient)128 IOException (java.io.IOException)78 FTPFile (org.apache.commons.net.ftp.FTPFile)36 Test (org.junit.Test)25 InputStream (java.io.InputStream)20 FrameworkException (org.structr.common.error.FrameworkException)20 Tx (org.structr.core.graph.Tx)20 FtpTest (org.structr.web.files.FtpTest)20 File (java.io.File)13 ByteArrayInputStream (java.io.ByteArrayInputStream)10 FileInputStream (java.io.FileInputStream)10 FileOutputStream (java.io.FileOutputStream)8 OutputStream (java.io.OutputStream)5 UnknownHostException (java.net.UnknownHostException)5 ConnectException (java.net.ConnectException)4 PrintCommandListener (org.apache.commons.net.PrintCommandListener)4 FTPSClient (org.apache.commons.net.ftp.FTPSClient)4 FTPUtils (com.cas.sim.tis.util.FTPUtils)3 FileNotFoundException (java.io.FileNotFoundException)3 MalformedURLException (java.net.MalformedURLException)3