org.apache.commons.net.ftp.FTPReply Java Examples

The following examples show how to use org.apache.commons.net.ftp.FTPReply. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: cfFTPData.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public boolean siteCmd(String cmd) {
	if ( !ftpclient.isConnected() ){
		errorText = "not connected";
		succeeded	= false;
		return false;
	}
	
	boolean bResult = false;
	try{
		bResult 	= ftpclient.sendSiteCommand(cmd);
		errorCode = ftpclient.getReplyCode();
		succeeded	= FTPReply.isPositiveCompletion(errorCode);
	}catch(Exception e){
		errorCode = ftpclient.getReplyCode();
		errorText	= e.getMessage();
	}finally{
		setStatusData();
	}
	
	return bResult;
}
 
Example #2
Source File: FtpConnection.java    From xian with Apache License 2.0 6 votes vote down vote up
FTPClient connect(String path, String addr, int port, String username, String password) throws Exception {
    FTPClient ftp = new FTPClient();
    int reply;
    ftp.connect(addr, port);
    ftp.login(username, password);
    ftp.configure(new FTPClientConfig(ftp.getSystemType()));
    ftp.setControlEncoding("UTF-8");
    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        return null;
    }
    ftp.changeWorkingDirectory(path);
    return ftp;
}
 
Example #3
Source File: FtpContinueClient.java    From spring-boot-study with MIT License 6 votes vote down vote up
/**
 * 连接到FTP服务器
 *
 * @param hostname 主机名
 * @param port 端口
 * @param username 用户名
 * @param password 密码
 * @return 是否连接成功
 * @throws IOException
 */
public boolean connect(String hostname, int port, String username, String password)
        throws Exception {

    try
    {
        ftpClient.connect(hostname, port);
    }
    catch (Exception e)
    {
        throw new Exception("登陆异常,请检查主机端口");
    }
    ftpClient.setControlEncoding("GBK");
    if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))
    {
        if (ftpClient.login(username, password))
        {
            return true;
        }
        else
            throw new Exception("登陆异常,请检查密码账号");
    }
    else
        throw new Exception("登陆异常");

}
 
Example #4
Source File: FTPUtil.java    From anyline with Apache License 2.0 6 votes vote down vote up
public void connect() { 
	try{ 
     client.connect(host, port);   
     int reply = client.getReplyCode();   
     if (!FTPReply.isPositiveCompletion(reply)) {   
         disconnect();   
     }   
     if ("".equals(account)) {   
     	account = "anonymous";   
     }   
     if (!client.login(account, password)) {   
         disconnect();   
     }   
     client.setFileType(FTP.BINARY_FILE_TYPE);   
     //ftp.setFileType(FTP.ASCII_FILE_TYPE);   
     client.enterLocalPassiveMode();  
	}catch(Exception e){ 
		e.printStackTrace(); 
	} 
       
}
 
Example #5
Source File: FtpRemoteFileSystem.java    From cloudhopper-commons with Apache License 2.0 6 votes vote down vote up
public boolean exists(String filename) throws FileSystemException {
    // we have to be connected
    if (ftp == null) {
        throw new FileSystemException("Not yet connected to FTP server");
    }

    try {
        // check if the file already exists
        FTPFile[] files = ftp.listFiles(filename);

        // did this command succeed?
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new FileSystemException("FTP server failed to get file listing (reply=" + ftp.getReplyString() + ")");
        }

        if (files != null && files.length > 0) {
            // this file already exists
            return true;
        } else {
            return false;
        }

    } catch (IOException e) {
        throw new FileSystemException("Underlying IO exception with FTP server while checking if file exists", e);
    }
}
 
Example #6
Source File: FTPExceptionMappingService.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
private BackgroundException handle(final FTPException e, final StringBuilder buffer) {
    final int status = e.getCode();
    switch(status) {
        case FTPReply.INSUFFICIENT_STORAGE:
        case FTPReply.STORAGE_ALLOCATION_EXCEEDED:
            return new QuotaException(buffer.toString(), e);
        case FTPReply.NOT_LOGGED_IN:
            return new LoginFailureException(buffer.toString(), e);
        case FTPReply.FAILED_SECURITY_CHECK:
        case FTPReply.DENIED_FOR_POLICY_REASONS:
        case FTPReply.NEED_ACCOUNT:
        case FTPReply.NEED_ACCOUNT_FOR_STORING_FILES:
        case FTPReply.FILE_NAME_NOT_ALLOWED:
        case FTPReply.FILE_ACTION_NOT_TAKEN:
        case FTPReply.ACTION_ABORTED:
            return new AccessDeniedException(buffer.toString(), e);
        case FTPReply.UNAVAILABLE_RESOURCE:
        case FTPReply.FILE_UNAVAILABLE:
            // Requested action not taken. File unavailable (e.g., file not found, no access)
            return new NotfoundException(buffer.toString(), e);
        case FTPReply.SERVICE_NOT_AVAILABLE:
            return new ConnectionRefusedException(buffer.toString(), e);
    }
    return new InteroperabilityException(buffer.toString(), e);
}
 
Example #7
Source File: FTPSNetworkClient.java    From FireFiles with Apache License 2.0 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
Source File: FTPNetworkClient.java    From FireFiles with Apache License 2.0 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
Source File: FTPNetworkClient.java    From FireFiles with Apache License 2.0 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 #10
Source File: FTPSNetworkClient.java    From FireFiles with Apache License 2.0 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 #11
Source File: FTPNetworkClient.java    From FireFiles with Apache License 2.0 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 #12
Source File: FtpFileUtil.java    From hsac-fitnesse-fixtures with Apache License 2.0 6 votes vote down vote up
/**
 * Connect and login on given FTP server with provided credentials.
 * @param hostName the FTP server host name to connect
 * @param port the port to connect
 * @param userName the user name
 * @param password the password
 */
public static void connectAndLoginOnFTPServer(FTPClient ftpClient,
        String hostName, Integer port, String userName, String password) {
    try {
        if (port != null && port.intValue() > 0) {
            ftpClient.connect(hostName, port);
        } else {
            ftpClient.connect(hostName);
        }
        if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
            throw new IOException(String.format("FTP server '%s' refused connection.", hostName));
        }

        // try to login
        if (!ftpClient.login(userName, password)) {
            throw new IOException(String.format("Unable to login to FTP server '%s'.", hostName));
        }
    } catch (IOException ex) {
        throw new RuntimeException(
                String.format("Unable to connect and login to FTP server '%s'. Cause: ", hostName), ex);
    }
}
 
Example #13
Source File: FtpClientService.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
public void connect(String hostname, int port, String username, String password) throws BrokerException {
    try {
        ftpClient.setControlEncoding("UTF-8");
        ftpClient.connect(hostname, port);

        if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
            if (ftpClient.login(username, password)) {
                this.defaultDir = ftpClient.printWorkingDirectory();
            } else {
                log.error("login to ftp failed, please checkout username and password!");
                throw new BrokerException(ErrorCode.FTP_INVALID_USERNAME_PASSWD);
            }
        } else {
            log.error("login to ftp failed, unknown error.");
            throw new BrokerException(ErrorCode.FTP_LOGIN_FAILED);
        }
    } catch (IOException e) {
        throw new BrokerException(e.getMessage());
    }
}
 
Example #14
Source File: Ftp.java    From Lottery with GNU General Public License v2.0 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 #15
Source File: cfFTPData.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public boolean removeFile(String file) {
	if ( !ftpclient.isConnected() ){
		errorText = "not connected";
		succeeded	= false;
		return false;
	}
	
	boolean bResult = false;
	try{
		bResult 	= ftpclient.deleteFile(file);
		errorCode = ftpclient.getReplyCode();
		succeeded	= FTPReply.isPositiveCompletion(errorCode);
	}catch(Exception e){
		errorCode = ftpclient.getReplyCode();
		errorText	= e.getMessage();
	}finally{
		setStatusData();
	}
	
	return bResult;
}
 
Example #16
Source File: cfFTPData.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public boolean setCurrentDirectory(String directory) {
	if ( !ftpclient.isConnected() ){
		errorText = "not connected";
		succeeded	= false;
		return false;
	}
	
	boolean bResult = false;
	try{
		bResult 	= ftpclient.changeWorkingDirectory(directory);
		errorCode = ftpclient.getReplyCode();
		succeeded	= FTPReply.isPositiveCompletion(errorCode);
	}catch(Exception e){
		errorCode = ftpclient.getReplyCode();
		errorText	= e.getMessage();
	}finally{
		setStatusData();
	}
	
	return bResult;
}
 
Example #17
Source File: cfFTPData.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public String getCurrentDirectory() {
	if ( !ftpclient.isConnected() ){
		errorText = "not connected";
		succeeded	= false;
		return null;
	}
	
	String curdir = null;
	try{
		curdir		= ftpclient.printWorkingDirectory();
		errorCode = ftpclient.getReplyCode();
		succeeded	= FTPReply.isPositiveCompletion(errorCode);
	}catch(Exception e){
		errorCode = ftpclient.getReplyCode();
		errorText	= e.getMessage();
	}finally{
		setStatusData();
	}
	
	return curdir;
}
 
Example #18
Source File: cfFTPData.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public FTPFile[]	listFiles(String directory) {
	if ( !ftpclient.isConnected() ){
		errorText = "not connected";
		succeeded	= false;
		return null;
	}
	
	FTPFile[]	files = null;
	
	try{
		files			= ftpclient.listFiles(directory);
		errorCode = ftpclient.getReplyCode();
		succeeded	= FTPReply.isPositiveCompletion(errorCode);
	}catch(Exception e){
		errorCode = ftpclient.getReplyCode();
		errorText	= e.getMessage();
	}finally{
		setStatusData();
	}
	
	return files;
}
 
Example #19
Source File: cfFTPData.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public void open(){
	if ( !ftpclient.isConnected() ){
		try {
			ftpclient.connect( getData("server").getString(),  getData("port").getInt() );
			errorCode = ftpclient.getReplyCode();
			
			if (!FTPReply.isPositiveCompletion(errorCode))
				throw new Exception( ftpclient.getReplyString() );

			succeeded = ftpclient.login(username, password);
			errorCode = ftpclient.getReplyCode();
			
		} catch (Exception e) {
			succeeded = false;
			errorText	= e.getMessage();
		}
		setStatusData();
	}
}
 
Example #20
Source File: FtpClientManager.java    From onetwo with Apache License 2.0 6 votes vote down vote up
public void changeAndMakeDirs(String dir){
	String ftpDir = dir;//FileUtils.replaceBackSlashToSlash(dir);
	File dirFile = new File(ftpDir);
	try {
		if(!ftpClient.changeWorkingDirectory(ftpDir)){
			if(ftpClient.getReplyCode()==550){
				changeAndMakeDirs(dirFile.getParent());
			}
			int rcode = ftpClient.mkd(dirFile.getName());
			if(!FTPReply.isPositiveCompletion(rcode)){
				throw new BaseException("cmd[mkd] reply code : "+rcode);
			}
		}
	} catch (IOException e) {
		throw new BaseException("create dir error: " + dir, e);
	}
}
 
Example #21
Source File: SimpleFtpClient.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
@Override
public void connect(String hostname, String username, String password, Long port, Long timeout) throws IOException, GeneralException {
    if (client == null) return;
    if (client.isConnected()) return;
    if (port != null) {
        client.connect(hostname, port.intValue());
    } else {
        client.connect(hostname);
    }
    if (timeout != null) client.setDefaultTimeout(timeout.intValue());

    if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
        Debug.logError("Server refused connection", module);
        throw new GeneralException(UtilProperties.getMessage("CommonUiLabels", "CommonFtpConnectionRefused", Locale.getDefault()));
    }
    if (!client.login(username, password)) {
        Debug.logError("login failed", module);
        throw new GeneralException(UtilProperties.getMessage("CommonUiLabels", "CommonFtpLoginFailure", Locale.getDefault()));
    }
}
 
Example #22
Source File: FTPClientTemplate.java    From MicroCommunity with Apache License 2.0 5 votes vote down vote up
/** 
 * 连接到ftp服务器 
 *  
 * @param ftpClient 
 * @return 连接成功返回true,否则返回false 
 * @throws Exception
 */  
private boolean connect(FTPClient ftpClient)throws Exception {
    try {  
        ftpClient.connect(host, port);  
  
        // 连接后检测返回码来校验连接是否成功  
        int reply = ftpClient.getReplyCode();  
  
        if (FTPReply.isPositiveCompletion(reply)) {
            //登陆到ftp服务器  
            if (ftpClient.login(username, password)) {  
                setFileType(ftpClient);  
                return true;  
            }  
        } else {  
            ftpClient.disconnect();  
            throw new Exception("FTP server refused connection.");
        }  
    } catch (IOException e) {
        if (ftpClient.isConnected()) {  
            try {  
                ftpClient.disconnect(); //断开连接  
            } catch (IOException e1) {
                throw new Exception("Could not disconnect from server.", e1);
            }  
  
        }  
        throw new Exception("Could not connect to server.", e);
    }  
    return false;  
}
 
Example #23
Source File: ConnectionStressTest.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    try {
        FTPSClient c = new FTPSClient();
        c.configure(ftpConfig);

        logger.debug("Trying to connect");
        c.connect("127.0.0.1", 2121);
        logger.debug("Connected");

        c.setSoTimeout(5000);

        if (!FTPReply.isPositiveCompletion(c.getReplyCode())) {
            logger.debug("Houston, we have a problem. D/C");
            c.disconnect();
            throw new Exception();
        }

        if (c.login("drftpd", "drftpd")) {
            logger.debug("Logged-in, now waiting 5 secs and kill the thread.");
            _sc.addSuccess();
            Thread.sleep(5000);
            c.disconnect();
        } else {
            logger.debug("Login failed, D/C!");
            throw new Exception();
        }
    } catch (Exception e) {
        logger.debug(e, e);
        _sc.addFailure();
    }

    logger.debug("exiting");
}
 
Example #24
Source File: RomLoader.java    From zxpoly with GNU General Public License v3.0 5 votes vote down vote up
static byte[] loadFTPArchive(final String host, final String path, final String name,
                             final String password) throws IOException {
  final FTPClient client = new FTPClient();
  client.connect(host);
  int replyCode = client.getReplyCode();

  if (FTPReply.isPositiveCompletion(replyCode)) {
    try {
      client.login(name == null ? "" : name, password == null ? "" : password);
      client.setFileType(FTP.BINARY_FILE_TYPE);
      client.enterLocalPassiveMode();

      final ByteArrayOutputStream out = new ByteArrayOutputStream(300000);
      if (client.retrieveFile(path, out)) {
        return out.toByteArray();
      } else {
        throw new IOException(
            "Can't load file 'ftp://" + host + path + "\' status=" + client.getReplyCode());
      }
    } finally {
      client.disconnect();
    }
  } else {
    client.disconnect();
    throw new IOException("Can't connect to ftp '" + host + "'");
  }
}
 
Example #25
Source File: FtpLogs.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
private void sendFiles() throws SocketException, IOException {
		ftpClient.connect(server, port);
		Log.println("Connected to " + server + ".");
	    Log.print(ftpClient.getReplyString());
	    int reply = ftpClient.getReplyCode();
	    if(!FTPReply.isPositiveCompletion(reply)) {
	    	  ftpClient.disconnect();
	        Log.println("FTP server refused connection.");
	    } else {
	    	ftpClient.login(user, pass);
	    	Log.println("Logging in..");
	    	Log.print(ftpClient.getReplyString());

	    	ftpClient.enterLocalPassiveMode();	
	    	ftpClient.setControlKeepAliveTimeout(300); // set timeout to 5 minutes.  Send NOOPs to keep control channel alive
	    	
	    	ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
	    	
	    	/**
	    	 * This is currently disabled because the payload store changed.  We probablly only want to send for one
	    	 * satellite as this is only used for debugging
	    	 * 
	    	sendFile(PayloadStore.RT_LOG);
	    	sendFile(PayloadStore.MAX_LOG);
	    	sendFile(PayloadStore.MIN_LOG);
	    	sendFile(PayloadStore.RAD_LOG);
*/
	    	ftpClient.disconnect();
	    }
	}
 
Example #26
Source File: FTPUploader.java    From journaldev with MIT License 5 votes vote down vote up
public FTPUploader(String host, String user, String pwd) throws Exception{
	ftp = new FTPClient();
	ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
	int reply;
	ftp.connect(host);
	reply = ftp.getReplyCode();
	if (!FTPReply.isPositiveCompletion(reply)) {
		ftp.disconnect();
		throw new Exception("Exception in connecting to FTP Server");
	}
	ftp.login(user, pwd);
	ftp.setFileType(FTP.BINARY_FILE_TYPE);
	ftp.enterLocalPassiveMode();
}
 
Example #27
Source File: FTPClientWrapper.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
private FTPFile[] listFilesInDirectory(final String relPath) throws IOException {
    FTPFile[] files;

    // VFS-307: no check if we can simply list the files, this might fail if there are spaces in the path
    files = getFtpClient().listFiles(relPath);
    if (FTPReply.isPositiveCompletion(getFtpClient().getReplyCode())) {
        return files;
    }

    // VFS-307: now try the hard way by cd'ing into the directory, list and cd back
    // if VFS is required to fallback here the user might experience a real bad FTP performance
    // as then every list requires 4 ftp commands.
    String workingDirectory = null;
    if (relPath != null) {
        workingDirectory = getFtpClient().printWorkingDirectory();
        if (!getFtpClient().changeWorkingDirectory(relPath)) {
            return null;
        }
    }

    files = getFtpClient().listFiles();

    if (relPath != null && !getFtpClient().changeWorkingDirectory(workingDirectory)) {
        throw new FileSystemException("vfs.provider.ftp.wrapper/change-work-directory-back.error",
                workingDirectory);
    }
    return files;
}
 
Example #28
Source File: FTPFileSystem.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #29
Source File: FtpClient.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized String getNegativeReplyString() {
    int replyCode = ftpClient.getReplyCode();
    if (FTPReply.isNegativePermanent(replyCode)
            || FTPReply.isNegativeTransient(replyCode)) {
        return getReplyString();
    }
    return null;
}
 
Example #30
Source File: FTPUtils.java    From TranskribusCore with GNU General Public License v3.0 5 votes vote down vote up
public static void connectToFTP(FTPSClient ftp, String ftpServer, int ftpPort) throws SocketException, IOException {
	ftp.connect(ftpServer, ftpPort);
	// After connection attempt, you should check the reply code to verify
	// success.
	int reply = ftp.getReplyCode();

	if (!FTPReply.isPositiveCompletion(reply)) {
		ftp.disconnect();
		throw new IOException("FTP server refused connection, reply code: " + reply);
	}
	logger.debug("Connected to " + ftpServer + "."+ftp.getReplyString());
}