Search in sources :

Example 16 with FTPClient

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

the class FTPFileSystem method open.

@Override
public FSDataInputStream open(Path file, int bufferSize) throws IOException {
    FTPClient client = connect();
    Path workDir = new Path(client.printWorkingDirectory());
    Path absolute = makeAbsolute(workDir, file);
    FileStatus fileStat = getFileStatus(client, absolute);
    if (fileStat.isDirectory()) {
        disconnect(client);
        throw new FileNotFoundException("Path " + file + " is a directory.");
    }
    client.allocate(bufferSize);
    Path parent = absolute.getParent();
    // Change to parent directory on the
    // server. Only then can we read the
    // file
    // on the server by opening up an InputStream. As a side effect the working
    // directory on the server is changed to the parent directory of the file.
    // The FTP client connection is closed when close() is called on the
    // FSDataInputStream.
    client.changeWorkingDirectory(parent.toUri().getPath());
    InputStream is = client.retrieveFileStream(file.getName());
    FSDataInputStream fis = new FSDataInputStream(new FTPInputStream(is, client, statistics));
    if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
        // The ftpClient is an inconsistent state. Must close the stream
        // which in turn will logout and disconnect from FTP server
        fis.close();
        throw new IOException("Unable to open file: " + file + ", Aborting");
    }
    return fis;
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 17 with FTPClient

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

the class FtpEndpoint method createFtpClient.

protected FTPClient createFtpClient() throws Exception {
    FTPClient client = new FTPClient();
    // default ParserFactory
    if (isOsgi()) {
        ClassResolver cr = getCamelContext().getClassResolver();
        OsgiParserFactory opf = new OsgiParserFactory(cr);
        client.setParserFactory(opf);
    }
    return client;
}
Also used : ClassResolver(org.apache.camel.spi.ClassResolver) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 18 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 19 with FTPClient

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

the class FtpBadLoginMockNoopConnectionLeakTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    FtpEndpoint<?> endpoint = context.getEndpoint(getFtpUrl(), FtpEndpoint.class);
    endpoint.setFtpClient(new FTPClient() {

        @Override
        public boolean sendNoOp() throws IOException {
            // return true as long as connection is established
            return this.isConnected();
        }
    });
}
Also used : IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 20 with FTPClient

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

the class StandardFtpHelper method loginFtpServer.

@Override
public void loginFtpServer(String host, String username, String password, int port, int timeout, String connectMode) {
    ftpClient = new FTPClient();
    try {
        // 连接
        ftpClient.connect(host, port);
        // 登录
        ftpClient.login(username, password);
        // 不需要写死ftp server的OS TYPE,FTPClient getSystemType()方法会自动识别
        // ftpClient.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX));
        ftpClient.setConnectTimeout(timeout);
        ftpClient.setDataTimeout(timeout);
        if ("PASV".equals(connectMode)) {
            ftpClient.enterRemotePassiveMode();
            ftpClient.enterLocalPassiveMode();
        } else if ("PORT".equals(connectMode)) {
            ftpClient.enterLocalActiveMode();
        // ftpClient.enterRemoteActiveMode(host, port);
        }
        int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            String message = String.format("与ftp服务器建立连接失败,请检查用户名和密码是否正确: [%s]", "message:host =" + host + ",username = " + username + ",port =" + port);
            LOG.error(message);
            throw DataXException.asDataXException(FtpReaderErrorCode.FAIL_LOGIN, message);
        }
        //设置命令传输编码
        String fileEncoding = System.getProperty("file.encoding");
        ftpClient.setControlEncoding(fileEncoding);
    } catch (UnknownHostException e) {
        String message = String.format("请确认ftp服务器地址是否正确,无法连接到地址为: [%s] 的ftp服务器", host);
        LOG.error(message);
        throw DataXException.asDataXException(FtpReaderErrorCode.FAIL_LOGIN, message, e);
    } catch (IllegalArgumentException e) {
        String message = String.format("请确认连接ftp服务器端口是否正确,错误的端口: [%s] ", port);
        LOG.error(message);
        throw DataXException.asDataXException(FtpReaderErrorCode.FAIL_LOGIN, message, e);
    } catch (Exception e) {
        String message = String.format("与ftp服务器建立连接失败 : [%s]", "message:host =" + host + ",username = " + username + ",port =" + port);
        LOG.error(message);
        throw DataXException.asDataXException(FtpReaderErrorCode.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)

Aggregations

FTPClient (org.apache.commons.net.ftp.FTPClient)27 IOException (java.io.IOException)12 InputStream (java.io.InputStream)3 Path (org.apache.hadoop.fs.Path)3 DataXException (com.alibaba.datax.common.exception.DataXException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 ConnectException (java.net.ConnectException)2 MalformedURLException (java.net.MalformedURLException)2 UnknownHostException (java.net.UnknownHostException)2 FTPFile (org.apache.commons.net.ftp.FTPFile)2 Configuration (org.apache.hadoop.conf.Configuration)2 FileStatus (org.apache.hadoop.fs.FileStatus)2 ConnectionString (com.microsoft.azure.management.appservice.ConnectionString)1 TrafficManagerAzureEndpoint (com.microsoft.azure.management.trafficmanager.TrafficManagerAzureEndpoint)1 TrafficManagerExternalEndpoint (com.microsoft.azure.management.trafficmanager.TrafficManagerExternalEndpoint)1