Search in sources :

Example 51 with FTPClient

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

the class ExportFiles method export.

public void export() {
    FileInputStream fis = null;
    try {
        File localFile = new File(this.filePath);
        switch(chosenOption) {
            case OPTION_FTP:
                FTPClient client = new FTPClient();
                client.connect(this.serverAdress, this.port);
                client.login(this.login, this.pwd);
                client.enterLocalPassiveMode();
                client.setFileType(FTPClient.BINARY_FILE_TYPE);
                String remoteFile = this.remotePath + this.remoteFileName;
                fis = new FileInputStream(localFile);
                client.storeFile(remoteFile, fis);
                client.logout();
                break;
            case OPTION_SFTP:
                StandardFileSystemManager manager = new StandardFileSystemManager();
                manager.init();
                FileObject localFileSFTP = manager.resolveFile(this.filePath);
                FileObject remoteFileSFTP = manager.resolveFile("sftp://" + this.login + ":" + this.pwd + "@" + this.serverAdress + ":" + this.port + this.remotePath + this.remoteFileName);
                // Copy local file to sftp server
                remoteFileSFTP.copyFrom(localFileSFTP, Selectors.SELECT_SELF);
                manager.close();
                break;
            case OPTION_WEBDAV:
                fis = new FileInputStream(localFile);
                Sardine sardine = SardineFactory.begin(this.login, this.pwd);
                sardine.put(this.serverAdress + this.remotePath + this.remoteFileName, fis);
                break;
            default:
                break;
        }
    } catch (IOException e) {
        // Getting the exception message, that we will give to VueAnon with getResult
        this.result = e.getMessage();
    } finally {
        try {
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : Sardine(com.github.sardine.Sardine) StandardFileSystemManager(org.apache.commons.vfs2.impl.StandardFileSystemManager) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 52 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project fess-crawler by codelibs.

the class FtpClient method getResponseData.

protected ResponseData getResponseData(final String uri, final boolean includeContent) {
    final ResponseData responseData = new ResponseData();
    FTPClient client = null;
    try {
        responseData.setMethod(Constants.GET_METHOD);
        final FtpInfo ftpInfo = new FtpInfo(uri);
        responseData.setUrl(ftpInfo.toUrl());
        client = getClient(ftpInfo);
        FTPFile file = null;
        client.changeWorkingDirectory(ftpInfo.getParent());
        validateRequest(client);
        if (ftpInfo.getName() == null) {
            // root directory
            final Set<RequestData> requestDataSet = new HashSet<>();
            if (includeContent) {
                try {
                    final FTPFile[] files = client.listFiles(ftpInfo.getParent(), FTPFileFilters.NON_NULL);
                    validateRequest(client);
                    for (final FTPFile f : files) {
                        final String chileUri = ftpInfo.toChildUrl(f.getName());
                        requestDataSet.add(RequestDataBuilder.newRequestData().get().url(chileUri).build());
                    }
                } catch (final IOException e) {
                    disconnectInternalClient(client);
                    throw new CrawlingAccessException("Could not access " + uri, e);
                }
            }
            ftpClientQueue.offer(client);
            throw new ChildUrlsException(requestDataSet, this.getClass().getName() + "#getResponseData");
        }
        final FTPFile[] files = client.listFiles(null, FTPFileFilters.NON_NULL);
        validateRequest(client);
        for (final FTPFile f : files) {
            if (ftpInfo.getName().equals(f.getName())) {
                file = f;
                break;
            }
        }
        updateResponseData(uri, includeContent, responseData, client, ftpInfo, file);
    } catch (final CrawlerSystemException e) {
        CloseableUtil.closeQuietly(responseData);
        throw e;
    } catch (final Exception e) {
        CloseableUtil.closeQuietly(responseData);
        throw new CrawlingAccessException("Could not access " + uri, e);
    }
    return responseData;
}
Also used : ChildUrlsException(org.codelibs.fess.crawler.exception.ChildUrlsException) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) ResponseData(org.codelibs.fess.crawler.entity.ResponseData) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) CrawlerLoginFailureException(org.codelibs.fess.crawler.exception.CrawlerLoginFailureException) MaxLengthExceededException(org.codelibs.fess.crawler.exception.MaxLengthExceededException) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ChildUrlsException(org.codelibs.fess.crawler.exception.ChildUrlsException) RequestData(org.codelibs.fess.crawler.entity.RequestData) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) HashSet(java.util.HashSet)

Example 53 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project fess-crawler by codelibs.

the class FtpClient method getClient.

protected FTPClient getClient(final FtpInfo info) throws IOException {
    FTPClient ftpClient = ftpClientQueue.poll();
    if (ftpClient != null) {
        if (ftpClient.isAvailable()) {
            return ftpClient;
        }
        try {
            ftpClient.disconnect();
        } catch (final Exception e) {
            logger.debug("Failed to disconnect " + info.toUrl(), e);
        }
    }
    try {
        ftpClient = new FTPClient();
        if (activeExternalHost != null) {
            ftpClient.setActiveExternalIPAddress(activeExternalHost);
        }
        if (passiveLocalHost != null) {
            ftpClient.setPassiveLocalIPAddress(passiveLocalHost);
        }
        if (reportActiveExternalHost != null) {
            ftpClient.setReportActiveExternalIPAddress(reportActiveExternalHost);
        }
        if (activeMinPort != -1 && activeMaxPort != -1) {
            ftpClient.setActivePortRange(activeMinPort, activeMaxPort);
        }
        ftpClient.setAutodetectUTF8(autodetectEncoding);
        ftpClient.setConnectTimeout(connectTimeout);
        ftpClient.setDataTimeout(dataTimeout);
        ftpClient.setControlEncoding(controlEncoding);
        ftpClient.setBufferSize(bufferSize);
        if (passiveNatWorkaround) {
            ftpClient.setPassiveNatWorkaroundStrategy(new NatServerResolverImpl(ftpClient));
        }
        ftpClient.setUseEPSVwithIPv4(useEPSVwithIPv4);
        ftpClient.configure(ftpClientConfig);
        ftpClient.connect(info.getHost(), info.getPort());
        validateRequest(ftpClient);
        final FtpAuthentication auth = ftpAuthenticationHolder.get(info.toUrl());
        if (auth != null) {
            if (!ftpClient.login(auth.getUsername(), auth.getPassword())) {
                throw new CrawlerLoginFailureException("Login Failure: " + auth.getUsername() + " for " + info.toUrl());
            }
        }
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        return ftpClient;
    } catch (final IOException e) {
        if (ftpClient != null) {
            try {
                ftpClient.disconnect();
            } catch (final Exception e1) {
                logger.debug("Failed to disconnect " + info.toUrl(), e);
            }
        }
        throw e;
    }
}
Also used : CrawlerLoginFailureException(org.codelibs.fess.crawler.exception.CrawlerLoginFailureException) NatServerResolverImpl(org.apache.commons.net.ftp.FTPClient.NatServerResolverImpl) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) CrawlerLoginFailureException(org.codelibs.fess.crawler.exception.CrawlerLoginFailureException) MaxLengthExceededException(org.codelibs.fess.crawler.exception.MaxLengthExceededException) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ChildUrlsException(org.codelibs.fess.crawler.exception.ChildUrlsException)

Example 54 with FTPClient

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

the class StandardFtpHelperImpl method loginFtpServer.

@Override
public void loginFtpServer(String host, String username, String password, int port, int timeout) {
    this.ftpClient = new FTPClient();
    try {
        this.ftpClient.setControlEncoding("UTF-8");
        // 不需要写死ftp server的OS TYPE,FTPClient getSystemType()方法会自动识别
        // this.ftpClient.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX));
        this.ftpClient.setDefaultTimeout(timeout);
        this.ftpClient.setConnectTimeout(timeout);
        this.ftpClient.setDataTimeout(timeout);
        // 连接登录
        this.ftpClient.connect(host, port);
        this.ftpClient.login(username, password);
        this.ftpClient.enterRemotePassiveMode();
        this.ftpClient.enterLocalPassiveMode();
        int reply = this.ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            this.ftpClient.disconnect();
            String message = String.format("与ftp服务器建立连接失败,host:%s, port:%s, username:%s, replyCode:%s", host, port, username, reply);
            LOG.error(message);
            throw DataXException.asDataXException(FtpWriterErrorCode.FAIL_LOGIN, message);
        }
    } catch (UnknownHostException e) {
        String message = String.format("请确认ftp服务器地址是否正确,无法连接到地址为: [%s] 的ftp服务器, errorMessage:%s", host, e.getMessage());
        LOG.error(message);
        throw DataXException.asDataXException(FtpWriterErrorCode.FAIL_LOGIN, message, e);
    } catch (IllegalArgumentException e) {
        String message = String.format("请确认连接ftp服务器端口是否正确,错误的端口: [%s], errorMessage:%s", port, e.getMessage());
        LOG.error(message);
        throw DataXException.asDataXException(FtpWriterErrorCode.FAIL_LOGIN, message, e);
    } catch (Exception e) {
        String message = String.format("与ftp服务器建立连接失败,host:%s, port:%s, username:%s, errorMessage:%s", host, port, username, e.getMessage());
        LOG.error(message);
        throw DataXException.asDataXException(FtpWriterErrorCode.FAIL_LOGIN, message, e);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) FTPClient(org.apache.commons.net.ftp.FTPClient) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DataXException(com.alibaba.datax.common.exception.DataXException)

Example 55 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project irontest by zheng-wang.

the class FTPTeststepRunner method put.

private void put(Endpoint endpoint, FtpPutRequest ftpPutRequest) throws IOException {
    String username = StringUtils.trimToEmpty(endpoint.getUsername());
    String remoteFilePath = StringUtils.trimToEmpty(ftpPutRequest.getRemoteFilePath());
    byte[] fileBytes;
    // validate arguments
    if ("".equals(username)) {
        throw new IllegalArgumentException("Username not specified in Endpoint.");
    } else if ("".equals(remoteFilePath)) {
        throw new IllegalArgumentException("Target File Path not specified.");
    }
    if (ftpPutRequest instanceof FtpPutRequestFileFromText) {
        FtpPutRequestFileFromText ftpPutRequestFileFromText = (FtpPutRequestFileFromText) ftpPutRequest;
        String fileContent = ftpPutRequestFileFromText.getFileContent();
        // validate arguments
        if ("".equals(StringUtils.trimToEmpty(fileContent))) {
            throw new IllegalArgumentException("No file content.");
        }
        fileBytes = fileContent.getBytes();
    } else {
        FtpPutRequestFileFromFile ftpPutRequestFileFromFile = (FtpPutRequestFileFromFile) ftpPutRequest;
        fileBytes = ftpPutRequestFileFromFile.getFileContent();
        // validate arguments
        if (fileBytes == null || fileBytes.length == 0) {
            throw new IllegalArgumentException("No file content.");
        }
    }
    FTPEndpointProperties endpointProperties = (FTPEndpointProperties) endpoint.getOtherProperties();
    String password = getDecryptedEndpointPassword();
    FTPClient ftpClient;
    if (endpointProperties.isUseSSL()) {
        ftpClient = new FTPSClient();
        ((FTPSClient) ftpClient).setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
    } else {
        ftpClient = new FTPClient();
    }
    ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
    ftpClient.addProtocolCommandListener(new ProtocolCommandListener() {

        @Override
        public void protocolCommandSent(ProtocolCommandEvent event) {
        }

        @Override
        public void protocolReplyReceived(ProtocolCommandEvent event) {
            if (FTPReply.isNegativePermanent(event.getReplyCode())) {
                throw new RuntimeException("Failed to put the file. " + event.getMessage());
            }
        }
    });
    try {
        ftpClient.connect(endpoint.getHost(), endpoint.getPort());
        ftpClient.login(username, password);
        ftpClient.enterLocalPassiveMode();
        if (endpointProperties.isUseSSL()) {
            ((FTPSClient) ftpClient).execPROT("P");
        }
        ftpClient.storeFile(remoteFilePath, new ByteArrayInputStream(fileBytes));
    } finally {
        ftpClient.disconnect();
    }
}
Also used : ProtocolCommandEvent(org.apache.commons.net.ProtocolCommandEvent) FTPClient(org.apache.commons.net.ftp.FTPClient) ByteArrayInputStream(java.io.ByteArrayInputStream) FTPEndpointProperties(io.irontest.models.endpoint.FTPEndpointProperties) FTPSClient(org.apache.commons.net.ftp.FTPSClient) PrintCommandListener(org.apache.commons.net.PrintCommandListener) ProtocolCommandListener(org.apache.commons.net.ProtocolCommandListener) PrintWriter(java.io.PrintWriter)

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