NutzCN Logo
问答 Apache Commons Net FTP各种超时,DOS FTP可用?
发布于 2721天前 作者 liaojian1014 2771 次浏览 复制 上一个帖子 下一个帖子
标签:
/**
	 * 获取FTP链接
	 * @return
	 */
	public FTPClient getFTPConnect(){
		Config ftpConfig = this.getConfigInfo();
		FTPClient ftpClient = new FTPClient();
		FTPClientConfig ftpClientConfig = new FTPClientConfig();  
        ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
        ftpClient.configure(ftpClientConfig); 
		try {
			if(ftpConfig.getFTP_PORT() != null && ftpConfig.getFTP_PORT() > 0){
				ftpClient.connect(ftpConfig.getFTP_IP(), ftpConfig.getFTP_PORT());
			}else{
				ftpClient.connect(ftpConfig.getFTP_IP());
			}
			ftpClient.login(ftpConfig.getFTP_USERNAME(), ftpConfig.getFTP_PASSWORD());
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {  
                ftpClient.disconnect();  
                System.out.println("未连接到FTP,用户名或密码错误。");
            }else{
            	ftpClient.setControlEncoding("UTF-8"); 
                ftpClient.enterLocalPassiveMode();  
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                ftpClient.setBufferSize(1024 * 2);  
                ftpClient.setDataTimeout(30 * 1000); 
                System.out.println("FTP登录成功!");
            }
		} catch (SocketException e) {  
            e.printStackTrace();  
            System.out.println("FTP的IP地址可能错误,请正确配置。");  
        } catch (IOException e) {  
            e.printStackTrace();  
            System.out.println("FTP的端口错误,请正确配置。");  
        } 
		return ftpClient;
	}
/**
	 * 关闭FTP服务器的连接
	 * @param ftpClient
	 */
	public void FTPDisConnect(FTPClient ftpClient){
		try {
			//退出FTP服务器 
			boolean reuslt = ftpClient.logout();
			if(reuslt) {  
                System.out.println("成功退出FTP服务器");  
            }
		} catch (Exception e) {
			e.printStackTrace();
		} finally {  
			if(ftpClient.isConnected()) {
				try {  
	                ftpClient.disconnect();// 关闭FTP服务器的连接  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	                System.out.println("关闭FTP服务器的连接异常!");  
	            } 
			}
        }
	}
public void backup(){
		//step1
		FTPClient ftpClient = this.getFTPConnect();
		try {
			System.out.println(config.toString());
			System.out.println(ftpClient.isConnected());
			FTPFile[] files = ftpClient.listFiles();
			for(FTPFile ftpFile : files){
				System.out.println(ftpFile.getName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			//step5
			this.FTPDisConnect(ftpClient);
		}
	}
11 回复
错误信息:
java.net.ConnectException: Connection timed out: connect
	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:579)
	at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:920)
	at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:785)
	at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3409)
	at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3339)
	at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3016)
	at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3069)
	at systemBackup.PassengerSignBackup.backup(PassengerSignBackup.java:39)
	at systemBackup.FTPBackup.main(FTPBackup.java:14)

服务器的防火墙

只开放21端口是不行的

需要20端口?

我本地用CMD可远程FTP上传文件

那只能debug一下_openDataConnection_方法了吧,看看它连哪个端口去了

谢谢,解决了

原因是啥?

端口限制,我在服务器防火墙添加了被动模式端口1024-65535

建议用固定的被动端口,而不是全部开放

添加回复
请先登陆
回到顶部