Java Code Examples for org.apache.commons.net.ftp.FTP

The following are top voted examples for showing how to use org.apache.commons.net.ftp.FTP. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: aos-FileCoreLibrary   File: Session.java   View source code 6 votes vote down vote up
public FTPClient getFTPClient(Uri uri) throws SocketException, IOException, AuthenticationException{
    NetworkCredentialsDatabase database = NetworkCredentialsDatabase.getInstance();
    Credential cred = database.getCredential(uri.toString());
    if(cred==null){
        cred = new Credential("anonymous","", buildKeyFromUri(uri).toString(), true);
    }
    FTPClient ftpclient = ftpClients.get(cred);
    if (ftpclient!=null && ftpclient.isConnected()){
        return ftpclient;
    }
    // Not previous session found, open a new one
    Log.d(TAG, "create new ftp session for "+uri);
    FTPClient ftp = getNewFTPClient(uri,FTP.BINARY_FILE_TYPE);
    if(ftp==null)
        return null;
    Uri key = buildKeyFromUri(uri);
    Log.d(TAG, "new ftp session created with key "+key);
    ftpClients.put(cred, ftp);
    return ftp;
}
 
Example 2
Project: aos-FileCoreLibrary   File: Session.java   View source code 6 votes vote down vote up
public FTPClient getFTPSClient(Uri uri) throws SocketException, IOException, AuthenticationException{
    NetworkCredentialsDatabase database = NetworkCredentialsDatabase.getInstance();
    Credential cred = database.getCredential(uri.toString());
    if(cred==null){
        cred = new Credential("anonymous","", buildKeyFromUri(uri).toString(), true);
    }
    FTPClient ftpclient = ftpsClients.get(cred);
    if (ftpclient!=null && ftpclient.isConnected()){
        return ftpclient;
    }
    // Not previous session found, open a new one
    Log.d(TAG, "create new ftp session for "+uri);
    FTPClient ftp = getNewFTPSClient(uri, FTP.BINARY_FILE_TYPE);
    if(ftp==null)
        return null;
    Uri key = buildKeyFromUri(uri);
    Log.d(TAG, "new ftp session created with key "+key);
    ftpsClients.put(cred, ftp);
    return ftp;
}
 
Example 3
Project: easyfilemanager   File: FTPSNetworkClient.java   View source code 6 votes vote down vote up
@Override
public boolean connectClient() throws IOException {
    boolean isLoggedIn = true;
    client.setAutodetectUTF8(true);
    client.setControlEncoding("UTF-8");
    client.connect(host, port);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.enterLocalPassiveMode();
    client.login(username, password);
    int reply = client.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        client.disconnect();
        LogUtils.LOGD(TAG, "Negative reply form FTP server, aborting, id was {}:"+ reply);
        //throw new IOException("failed to connect to FTP server");
        isLoggedIn = false;
    }
    return isLoggedIn;
}
 
Example 4
Project: easyfilemanager   File: FTPNetworkClient.java   View source code 6 votes vote down vote up
@Override
public boolean connectClient() throws IOException {
    boolean isLoggedIn = true;
    client.setAutodetectUTF8(true);
    client.setControlEncoding("UTF-8");
    client.connect(host, port);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.enterLocalPassiveMode();
    client.login(username, password);
    int reply = client.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        client.disconnect();
        LogUtils.LOGD(TAG, "Negative reply form FTP server, aborting, id was {}:"+ reply);
        //throw new IOException("failed to connect to FTP server");
        isLoggedIn = false;
    }
    return isLoggedIn;
}
 
Example 5
Project: FireFiles   File: FTPSNetworkClient.java   View source code 6 votes vote down vote up
@Override
public boolean connectClient() throws IOException {
    boolean isLoggedIn = true;
    client.setAutodetectUTF8(true);
    client.setControlEncoding("UTF-8");
    client.connect(host, port);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.enterLocalPassiveMode();
    client.login(username, password);
    int reply = client.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        client.disconnect();
        LogUtils.LOGD(TAG, "Negative reply form FTP server, aborting, id was {}:"+ reply);
        //throw new IOException("failed to connect to FTP server");
        isLoggedIn = false;
    }
    return isLoggedIn;
}
 
Example 6
Project: FireFiles   File: FTPNetworkClient.java   View source code 6 votes vote down vote up
@Override
public boolean connectClient() throws IOException {
    boolean isLoggedIn = true;
    client.setAutodetectUTF8(true);
    client.setControlEncoding("UTF-8");
    client.connect(host, port);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.enterLocalPassiveMode();
    client.login(username, password);
    int reply = client.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        client.disconnect();
        LogUtils.LOGD(TAG, "Negative reply form FTP server, aborting, id was {}:"+ reply);
        //throw new IOException("failed to connect to FTP server");
        isLoggedIn = false;
    }
    return isLoggedIn;
}
 
Example 7
Project: simple-share-android   File: FTPSNetworkClient.java   View source code 6 votes vote down vote up
@Override
public boolean connectClient() throws IOException {
    boolean isLoggedIn = true;
    client.setAutodetectUTF8(true);
    client.setControlEncoding("UTF-8");
    client.connect(host, port);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.enterLocalPassiveMode();
    client.login(username, password);
    int reply = client.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        client.disconnect();
        LogUtils.LOGD(TAG, "Negative reply form FTP server, aborting, id was {}:"+ reply);
        //throw new IOException("failed to connect to FTP server");
        isLoggedIn = false;
    }
    return isLoggedIn;
}
 
Example 8
Project: simple-share-android   File: FTPNetworkClient.java   View source code 6 votes vote down vote up
@Override
public boolean connectClient() throws IOException {
    boolean isLoggedIn = true;
    client.setAutodetectUTF8(true);
    client.setControlEncoding("UTF-8");
    client.connect(host, port);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.enterLocalPassiveMode();
    client.login(username, password);
    int reply = client.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        client.disconnect();
        LogUtils.LOGD(TAG, "Negative reply form FTP server, aborting, id was {}:"+ reply);
        //throw new IOException("failed to connect to FTP server");
        isLoggedIn = false;
    }
    return isLoggedIn;
}
 
Example 9
Project: elegant-springboot   File: MyFtpClient.java   View source code 6 votes vote down vote up
private FTPClient initFtpClient(String remoteDir) throws IOException {
    FTPClient ftp = new FTPClient();
    // 设置连接超时时间
    ftp.setConnectTimeout(CONNECT_TIMEOUT);
    // 设置传输文件名编码方式
    ftp.setControlEncoding(CONTROL_ENCODING);
    ftp.connect(host, ftpPort);
    int reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        logger.debug("无法连接FTP");
        return null;
    }
    boolean loginRet = ftp.login(ftpUsername, ftpPassword);
    if (!loginRet) {
        logger.debug("FTP登录失败");
        return null;
    }
    // 进入被动模式
    ftp.enterLocalPassiveMode();
    boolean changeDirResult = MKDAndCWD(ftp, remoteDir);
    if (!changeDirResult) {
        logger.debug("创建/切换文件夹失败");
        return null;
    }
    // 传输二进制文件
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    return ftp;
}
 
Example 10
Project: aliyun-oss-hadoop-fs   File: FTPFileSystem.java   View source code 6 votes vote down vote up
/**
 * Connect to the FTP server using configuration parameters *
 * 
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
  FTPClient client = null;
  Configuration conf = getConf();
  String host = conf.get(FS_FTP_HOST);
  int port = conf.getInt(FS_FTP_HOST_PORT, FTP.DEFAULT_PORT);
  String user = conf.get(FS_FTP_USER_PREFIX + host);
  String password = conf.get(FS_FTP_PASSWORD_PREFIX + host);
  client = new FTPClient();
  client.connect(host, port);
  int reply = client.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
    throw NetUtils.wrapException(host, port,
                 NetUtils.UNKNOWN_HOST, 0,
                 new ConnectException("Server response " + reply));
  } else if (client.login(user, password)) {
    client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.setBufferSize(DEFAULT_BUFFER_SIZE);
  } else {
    throw new IOException("Login failed on server - " + host + ", port - "
        + port + " as user '" + user + "'");
  }

  return client;
}
 
Example 11
Project: YalpStore   File: BugReportSenderFtp.java   View source code 6 votes vote down vote up
private boolean uploadAll() {
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect(FTP_HOST, FTP_PORT);
        if (!ftpClient.login(FTP_USER, FTP_PASSWORD)) {
            return false;
        }
        String dirName = getDirName();
        ftpClient.makeDirectory(dirName);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
        ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
        boolean result = true;
        for (File file: files) {
            result &= upload(ftpClient, file, dirName + "/" + file.getName());
        }
        return result;
    } catch (IOException e) {
        Log.e(BugReportSenderFtp.class.getSimpleName(), "FTP network error: " + e.getMessage());
    } finally {
        closeSilently(ftpClient);
    }
    return false;
}
 
Example 12
Project: codePay   File: FtpClientUtils.java   View source code 6 votes vote down vote up
/**
 * 连接FTP服务器
 * @param host FTP服务器地址
 * @param port FTP服务器端口号
 * @param username 用户名
 * @param password 密码
 * @return
 */
public  static boolean connect(String host, int port, String username, String password) {
    try{
    	 ftpClient  = new FTPClient();
        ftpClient.connect(host, port);
        // 登录
        ftpClient.login(username, password);
        ftpClient.setBufferSize(FTP_SERVER_BUFFER_SIZE_VALUE);  
		ftpClient.setControlEncoding(FTP_CLIENT_ENCODING);
		ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
	   	ftpClient.setDataTimeout(FTP_SERVER_DATA_TIME_OUT);
    	ftpClient.setSoTimeout(FTP_SERVER_SO_TIME_OUT);
    	// 检验是否连接成功
        int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
        	LOGGER.error("连接FTP服务器失败,响应码:"+reply);
        	disConnectionServer();
            return false;
        }
    }catch(IOException e){
    	LOGGER.error("连接FTP服务器失败",e);
        return false;
    }
    return true;
}
 
Example 13
Project: SeedboxSync   File: Server.java   View source code 6 votes vote down vote up
/**
 * Opens the Server connection using the specified IP address and the default port.
 * @throws Exception Thrown if an exception is encountered during the connect or login operations.
 */
public void connect() throws Exception {
    logger.info("Connecting to '" + address + "'...");
    server.connect(address, port);

    logger.info("Logging in with credentials '" + username + "', '<hidden>'");
    server.login(username, password);

    logger.info("Configuring connection...");
    server.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
    server.setBufferSize(1024 * 1024);
    server.setAutodetectUTF8(true);
    server.setControlKeepAliveTimeout(300);

    logger.info("Connection established.");
}
 
Example 14
Project: atmosphere-agent   File: FtpConnectionManager.java   View source code 6 votes vote down vote up
/**
 * Connects the client to the FTP Server if is not already connected.
 */
public void connectToFtpServer() {
    try {
        ftpClient.connect(ftpServerName, port);
        ftpClient.login(username, password);

        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.changeWorkingDirectory(ftpHomeDirectory);

        if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
            throw new IOException("FTP server refused connection.");
        }

        LOGGER.info(String.format("Connected to an FTP Server with IP (%s:%s).", ftpServerName, port));
    } catch (IOException e) {
        LOGGER.error(String.format("Failed to connect to an FTP Server with IP (%s:%s).", ftpServerName, port), e);
    }
}
 
Example 15
Project: big-c   File: FTPFileSystem.java   View source code 6 votes vote down vote up
/**
 * Connect to the FTP server using configuration parameters *
 * 
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
  FTPClient client = null;
  Configuration conf = getConf();
  String host = conf.get(FS_FTP_HOST);
  int port = conf.getInt(FS_FTP_HOST_PORT, FTP.DEFAULT_PORT);
  String user = conf.get(FS_FTP_USER_PREFIX + host);
  String password = conf.get(FS_FTP_PASSWORD_PREFIX + host);
  client = new FTPClient();
  client.connect(host, port);
  int reply = client.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
    throw NetUtils.wrapException(host, port,
                 NetUtils.UNKNOWN_HOST, 0,
                 new ConnectException("Server response " + reply));
  } else if (client.login(user, password)) {
    client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.setBufferSize(DEFAULT_BUFFER_SIZE);
  } else {
    throw new IOException("Login failed on server - " + host + ", port - "
        + port + " as user '" + user + "'");
  }

  return client;
}
 
Example 16
Project: subs   File: FTPChecklistValidatorFactoryImpl.java   View source code 6 votes vote down vote up
private static InputStream getFTPInputStream(String ftpServerName, String checklistRemoteFilePath) throws IOException {
    FTPClient ftp = new FTPClient();
    ftp.connect(ftpServerName);
    int reply = ftp.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        throw new IOException("FTP server " + ftpServerName + " refused connection");
    }

    if (!ftp.login(USERNAME, PASSWORD)) {
        ftp.logout();
        throw new IOException("FTP server failed to login using username anonymous");
    }
    ftp.setFileType(FTP.ASCII_FILE_TYPE);
    InputStream inputStream = ftp.retrieveFileStream(checklistRemoteFilePath);
    return inputStream;
}
 
Example 17
Project: TranskribusSwtGui   File: FTPProgramUpdater.java   View source code 6 votes vote down vote up
private Pair<String, Date> getVersionAndTimestamp(String path) throws SocketException, IOException, InterruptedException, ParseException {
	try (MyFTPClient ftp = new MyFTPClient(ftpServer, ftpPort, ftpUser, ftpPw)) {		
		for (FTPFile f : ftp.listFiles(path)) {
			if (f.getName().equals(ProgramPackageFile.BUILD_FN)) {
				File tsF = new File("./updateTimestamp.txt");
				ftp.downloadFile(path, f, tsF, FTP.ASCII_FILE_TYPE);
				Properties p = new Properties();
				FileInputStream fis = new FileInputStream(tsF);
				p.load(fis);
				fis.close();
				tsF.delete();

				logger.debug("remote timestamp string: "+p.getProperty("Date"));
				return Pair.of(p.getProperty("Version"), CoreUtils.DATE_FORMAT.parse(p.getProperty("Date")));
			}
		}
	}
	
	return null;
}
 
Example 18
Project: TranskribusSwtGui   File: FTPProgramUpdater.java   View source code 6 votes vote down vote up
@Override
public void downloadUpdate(final File downloadFile, FTPProgramPackageFile f, final IProgressMonitor monitor, boolean downloadAll) throws Exception {
	monitor.beginTask("Downloading "+f.f.getName(), 100);
	// TODO: use FTPUtils.downloadFile method...
	FTPTransferListener transferL = new FTPTransferListener(f.f) {
		@Override public void downloaded(int percent) {
			monitor.worked(percent);
			monitor.subTask(percent+"%");											
			if (monitor.isCanceled()) {
				boolean success = abort();
			}
		}
	};
	
	try {
		FTPUtils.downloadFile(ftpServer, ftpPort, ftpUser, ftpPw, 
				f.path, FTP.BINARY_FILE_TYPE, f.f, downloadFile, transferL);
	} catch (IOException e) {
		throw new InvocationTargetException(e, e.getMessage());
	}
	
	monitor.done();
}
 
Example 19
Project: CatalogAutoExportKits_Huiwen   File: FtpHelper.java   View source code 6 votes vote down vote up
public boolean connect(String hostname,int port,String username,String password) throws IOException{   
    ftpClient.setConnectTimeout(30*1000);
	ftpClient.connect(hostname, port); 
    //ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); 
    if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){   
        if(ftpClient.login(username, password)){  
        	logger.info("login to [{}] with username[{}] password=[{}] success",hostname,username,password);
            ftpClient.enterLocalPassiveMode();   
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
        	return true;   
        } 
        else{  
        	logger.error("unable to login to ftp server[{}] with username[{}] password=[{}], abort!",hostname,username,password );
	        disconnect();   
        }
    }
    return false;   
}
 
Example 20
Project: PdfUtil   File: FTPUtil.java   View source code 6 votes vote down vote up
private void login() throws IOException {
	if(!isConnected()){
		connect();
	}
	
	ftpClient.login(user, password);
	
	if(passiveMode){
		ftpClient.enterLocalPassiveMode();
	}
	
	//PDF transmitted with ASCII file type break the file 
	ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
	ftpClient.setBufferSize(0);
	ftpClient.setControlKeepAliveTimeout(timeOutInSeconds);
}
 
Example 21
Project: hadoop-2.6.0-cdh5.4.3   File: FTPFileSystem.java   View source code 6 votes vote down vote up
/**
 * Connect to the FTP server using configuration parameters *
 * 
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
  FTPClient client = null;
  Configuration conf = getConf();
  String host = conf.get(FS_FTP_HOST);
  int port = conf.getInt(FS_FTP_HOST_PORT, FTP.DEFAULT_PORT);
  String user = conf.get(FS_FTP_USER_PREFIX + host);
  String password = conf.get(FS_FTP_PASSWORD_PREFIX + host);
  client = new FTPClient();
  client.connect(host, port);
  int reply = client.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
    throw NetUtils.wrapException(host, port,
                 NetUtils.UNKNOWN_HOST, 0,
                 new ConnectException("Server response " + reply));
  } else if (client.login(user, password)) {
    client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.setBufferSize(DEFAULT_BUFFER_SIZE);
  } else {
    throw new IOException("Login failed on server - " + host + ", port - "
        + port + " as user '" + user + "'");
  }

  return client;
}
 
Example 22
Project: hadoop-EAR   File: FTPFileSystem.java   View source code 6 votes vote down vote up
/**
 * Connect to the FTP server using configuration parameters *
 * 
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
  FTPClient client = null;
  Configuration conf = getConf();
  String host = conf.get("fs.ftp.host");
  int port = conf.getInt("fs.ftp.host.port", FTP.DEFAULT_PORT);
  String user = conf.get("fs.ftp.user." + host);
  String password = conf.get("fs.ftp.password." + host);
  client = new FTPClient();
  client.connect(host, port);
  int reply = client.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
    throw new IOException("Server - " + host
        + " refused connection on port - " + port);
  } else if (client.login(user, password)) {
    client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.setBufferSize(DEFAULT_BUFFER_SIZE);
  } else {
    throw new IOException("Login failed on server - " + host + ", port - "
        + port);
  }

  return client;
}
 
Example 23
Project: omakase   File: FtpProtocolHandler.java   View source code 6 votes vote down vote up
private void connect(FtpConnectionParameters param) {
    ftpClient = new FTPClient();
    try {
        ftpClient.setBufferSize(0);
        ftpClient.connect(param.getServer(), param.getPort());
        LOGGER.debug(ftpClient.getReplyString());
        ftpClient.login(param.getUser(), param.getPassword());
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        if (param.isPassive()) {
            ftpClient.enterLocalPassiveMode();
        }

    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        throw new ProtocolHandlerException("FTPIllegalReplyException: " + e.getMessage(), e.getCause());
    }
}
 
Example 24
Project: hadoop-plus   File: FTPFileSystem.java   View source code 6 votes vote down vote up
/**
 * Connect to the FTP server using configuration parameters *
 * 
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
  FTPClient client = null;
  Configuration conf = getConf();
  String host = conf.get("fs.ftp.host");
  int port = conf.getInt("fs.ftp.host.port", FTP.DEFAULT_PORT);
  String user = conf.get("fs.ftp.user." + host);
  String password = conf.get("fs.ftp.password." + host);
  client = new FTPClient();
  client.connect(host, port);
  int reply = client.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
    throw new IOException("Server - " + host
        + " refused connection on port - " + port);
  } else if (client.login(user, password)) {
    client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.setBufferSize(DEFAULT_BUFFER_SIZE);
  } else {
    throw new IOException("Login failed on server - " + host + ", port - "
        + port);
  }

  return client;
}
 
Example 25
Project: compomics-utilities   File: FTPDownloader.java   View source code 6 votes vote down vote up
/**
 * Set up an FTP connection.
 *
 * @param host the FTP host
 * @param user the FTP user name
 * @param pwd the FTP password
 * @param debug if true, the FTP protocol commands are printed
 * @throws Exception thrown if the connection could not be made
 */
public FTPDownloader(String host, String user, String pwd, boolean debug) throws Exception {
    ftp = new FTPClient();
    if (debug) {
        ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
    }

    ftp.connect(host);
    int reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        throw new Exception("Could not connect to FTP Server.");
    }
    ftp.login(user, pwd);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    ftp.enterLocalPassiveMode();
}
 
Example 26
Project: jeecms6   File: Ftp.java   View source code 6 votes vote down vote up
private FTPClient getClient() throws SocketException, IOException {
	FTPClient ftp = new FTPClient();
	ftp.addProtocolCommandListener(new PrintCommandListener(
			new PrintWriter(System.out)));
	ftp.setDefaultPort(getPort());
	ftp.connect(getIp());
	int reply = ftp.getReplyCode();
	if (!FTPReply.isPositiveCompletion(reply)) {
		log.warn("FTP server refused connection: {}", getIp());
		ftp.disconnect();
		return null;
	}
	if (!ftp.login(getUsername(), getPassword())) {
		log.warn("FTP server refused login: {}, user: {}", getIp(),
				getUsername());
		ftp.logout();
		ftp.disconnect();
		return null;
	}
	ftp.setControlEncoding(getEncoding());
	ftp.setFileType(FTP.BINARY_FILE_TYPE);
	ftp.enterLocalPassiveMode();
	return ftp;
}
 
Example 27
Project: Lucee4   File: FTPResource.java   View source code 6 votes vote down vote up
@Override
public OutputStream getOutputStream(boolean append) throws IOException {
	ResourceUtil.checkGetOutputStreamOK(this);
	FTPResourceClient client=null;
	try {
		provider.lock(this);
		client=provider.getClient(data);
		client.unregisterFTPFile(this);
		client.setFileType(FTP.BINARY_FILE_TYPE);
		OutputStream os = append?client.appendFileStream(getInnerPath()):client.storeFileStream(getInnerPath());
		if(os==null)throw new IOException("can not open stream to file ["+this+"]");
		
		return IOUtil.toBufferedOutputStream(new FTPResourceOutputStream(client,this,os));
	}
	catch (IOException e) {
		provider.returnClient(client);
		provider.unlock(this);
		throw e;
	}
}
 
Example 28
Project: hops   File: FTPFileSystem.java   View source code 6 votes vote down vote up
/**
 * Connect to the FTP server using configuration parameters *
 * 
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
  FTPClient client = null;
  Configuration conf = getConf();
  String host = conf.get(FS_FTP_HOST);
  int port = conf.getInt(FS_FTP_HOST_PORT, FTP.DEFAULT_PORT);
  String user = conf.get(FS_FTP_USER_PREFIX + host);
  String password = conf.get(FS_FTP_PASSWORD_PREFIX + host);
  client = new FTPClient();
  client.connect(host, port);
  int reply = client.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
    throw NetUtils.wrapException(host, port,
                 NetUtils.UNKNOWN_HOST, 0,
                 new ConnectException("Server response " + reply));
  } else if (client.login(user, password)) {
    client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.setBufferSize(DEFAULT_BUFFER_SIZE);
  } else {
    throw new IOException("Login failed on server - " + host + ", port - "
        + port + " as user '" + user + "'");
  }

  return client;
}
 
Example 29
Project: cordova-mover   File: BaseMover.java   View source code 6 votes vote down vote up
private void _connectFtp(String user, String password, String host, int port, IMoverInterface callbackContext) throws IOException {
    FTPClient ftpClient = new FTPClient();
    ftpClient.connect(host, port);

    int replyCode = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(replyCode)) {
        callbackContext.error("Operation failed. Server reply code: \" + replyCode");
        return;
    }

    boolean success = ftpClient.login(user, password);

    if (!success) {
        callbackContext.error("Bad username or password");
        return;
    }

    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    ftpClient.enterLocalPassiveMode();

    String key = UUID.randomUUID().toString();
    mFtpChannels.put(key, ftpClient);

    callbackContext.success(key);
}
 
Example 30
Project: Lucee   File: FTPResource.java   View source code 6 votes vote down vote up
@Override
public OutputStream getOutputStream(boolean append) throws IOException {
	ResourceUtil.checkGetOutputStreamOK(this);
	FTPResourceClient client=null;
	try {
		provider.lock(this);
		client=provider.getClient(data);
		client.unregisterFTPFile(this);
		client.setFileType(FTP.BINARY_FILE_TYPE);
		OutputStream os = append?client.appendFileStream(getInnerPath()):client.storeFileStream(getInnerPath());
		if(os==null)throw new IOException("can not open stream to file ["+this+"]");
		
		return IOUtil.toBufferedOutputStream(new FTPResourceOutputStream(client,this,os));
	}
	catch (IOException e) {
		provider.returnClient(client);
		provider.unlock(this);
		throw e;
	}
}
 
Example 31
Project: StormCV   File: FtpConnector.java   View source code 6 votes vote down vote up
private void checkAndConnect() throws IOException{
	if(client == null) client = new FTPClient();
	if(!client.isConnected()){ 
		if(location.getPort() == -1){
			client.connect(location.getHost());
		}else{
			client.connect(location.getHost(), location.getPort());
		}
		if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            throw new IOException("Exception in connecting to FTP Server");
        }
		
		if(username != null && password != null) client.login(username, password);
		client.setFileType(FTP.BINARY_FILE_TYPE);		
	}
}
 
Example 32
Project: itmarry   File: FtpClient.java   View source code 6 votes vote down vote up
/**
 * 上传文件
 * @param local
 * @param remote
 * @return
 */
public boolean uploadFile(String local, String remote){
	boolean result = false;
	if (FtpConfig.MODE_LOCAL_PASSIVE == config.getMode()) {
		client.enterLocalPassiveMode();
	}
	try {
		client.setFileStructure(FTP.FILE_STRUCTURE);
		client.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
		client.setFileType(FTP.BINARY_FILE_TYPE);
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return result;
}
 
Example 33
Project: itmarry   File: FtpClient.java   View source code 6 votes vote down vote up
/**
 * 下载文件
 * @param local
 * @param remote
 * @return
 */
public boolean downloadFile(String local, String remote){
	boolean result = false;
	if (FtpConfig.MODE_LOCAL_PASSIVE == config.getMode()) {
		client.enterLocalPassiveMode();
	}
	try {
		client.setFileStructure(FTP.FILE_STRUCTURE);
		client.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
		client.setFileType(FTP.BINARY_FILE_TYPE);
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return result;
}
 
Example 34
Project: hadoop-TCP   File: FTPFileSystem.java   View source code 6 votes vote down vote up
/**
 * Connect to the FTP server using configuration parameters *
 * 
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
  FTPClient client = null;
  Configuration conf = getConf();
  String host = conf.get("fs.ftp.host");
  int port = conf.getInt("fs.ftp.host.port", FTP.DEFAULT_PORT);
  String user = conf.get("fs.ftp.user." + host);
  String password = conf.get("fs.ftp.password." + host);
  client = new FTPClient();
  client.connect(host, port);
  int reply = client.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
    throw new IOException("Server - " + host
        + " refused connection on port - " + port);
  } else if (client.login(user, password)) {
    client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.setBufferSize(DEFAULT_BUFFER_SIZE);
  } else {
    throw new IOException("Login failed on server - " + host + ", port - "
        + port);
  }

  return client;
}
 
Example 35
Project: MALSignatureDesigner   File: Http.java   View source code 6 votes vote down vote up
public static void uploadToFTP(String domain, String saveTo, String user, String password, BufferedImage image){
	FTPClient client = new FTPClient();
    InputStream fis = null;

    try {
        client.connect(domain);
        client.login(user, password);
        client.setFileType(FTP.BINARY_FILE_TYPE);
    	client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
    	client.enterLocalPassiveMode();
        fis = imageToStream(image);

        client.storeFile(saveTo, fis);
        fis.close();
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 36
Project: hadoop-on-lustre   File: FTPFileSystem.java   View source code 6 votes vote down vote up
/**
 * Connect to the FTP server using configuration parameters *
 * 
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
  FTPClient client = null;
  Configuration conf = getConf();
  String host = conf.get("fs.ftp.host");
  int port = conf.getInt("fs.ftp.host.port", FTP.DEFAULT_PORT);
  String user = conf.get("fs.ftp.user." + host);
  String password = conf.get("fs.ftp.password." + host);
  client = new FTPClient();
  client.connect(host, port);
  int reply = client.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
    throw new IOException("Server - " + host
        + " refused connection on port - " + port);
  } else if (client.login(user, password)) {
    client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.setBufferSize(DEFAULT_BUFFER_SIZE);
  } else {
    throw new IOException("Login failed on server - " + host + ", port - "
        + port);
  }

  return client;
}
 
Example 37
Project: hardfs   File: FTPFileSystem.java   View source code 6 votes vote down vote up
/**
 * Connect to the FTP server using configuration parameters *
 * 
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
  FTPClient client = null;
  Configuration conf = getConf();
  String host = conf.get("fs.ftp.host");
  int port = conf.getInt("fs.ftp.host.port", FTP.DEFAULT_PORT);
  String user = conf.get("fs.ftp.user." + host);
  String password = conf.get("fs.ftp.password." + host);
  client = new FTPClient();
  client.connect(host, port);
  int reply = client.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
    throw new IOException("Server - " + host
        + " refused connection on port - " + port);
  } else if (client.login(user, password)) {
    client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.setBufferSize(DEFAULT_BUFFER_SIZE);
  } else {
    throw new IOException("Login failed on server - " + host + ", port - "
        + port);
  }

  return client;
}
 
Example 38
Project: hadoop-on-lustre2   File: FTPFileSystem.java   View source code 6 votes vote down vote up
/**
 * Connect to the FTP server using configuration parameters *
 * 
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
  FTPClient client = null;
  Configuration conf = getConf();
  String host = conf.get("fs.ftp.host");
  int port = conf.getInt("fs.ftp.host.port", FTP.DEFAULT_PORT);
  String user = conf.get("fs.ftp.user." + host);
  String password = conf.get("fs.ftp.password." + host);
  client = new FTPClient();
  client.connect(host, port);
  int reply = client.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
    throw new IOException("Server - " + host
        + " refused connection on port - " + port);
  } else if (client.login(user, password)) {
    client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.setBufferSize(DEFAULT_BUFFER_SIZE);
  } else {
    throw new IOException("Login failed on server - " + host + ", port - "
        + port);
  }

  return client;
}
 
Example 39
Project: arquillian-cube   File: FtpClient.java   View source code 6 votes vote down vote up
public FtpClient(String host, int port, String user, String password) throws Exception {

        ftpClient = new FTPClient();
        int reply;
        ftpClient.connect(host, port);
        reply = ftpClient.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            throw new Exception("Exception in connecting to FTP Server");
        }
        ftpClient.login(user, password);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        if ("localhost".equals(host)) {
            ftpClient.enterLocalPassiveMode();
        } else {
            ftpClient.enterLocalActiveMode();
        }
    }
 
Example 40
Project: RDFS   File: FTPFileSystem.java   View source code 6 votes vote down vote up
/**
 * Connect to the FTP server using configuration parameters *
 * 
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
  FTPClient client = null;
  Configuration conf = getConf();
  String host = conf.get("fs.ftp.host");
  int port = conf.getInt("fs.ftp.host.port", FTP.DEFAULT_PORT);
  String user = conf.get("fs.ftp.user." + host);
  String password = conf.get("fs.ftp.password." + host);
  client = new FTPClient();
  client.connect(host, port);
  int reply = client.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
    throw new IOException("Server - " + host
        + " refused connection on port - " + port);
  } else if (client.login(user, password)) {
    client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.setBufferSize(DEFAULT_BUFFER_SIZE);
  } else {
    throw new IOException("Login failed on server - " + host + ", port - "
        + port);
  }

  return client;
}