apache ftpClient 多网卡上传文件超时解决。

             在服务器中有多个网卡,默认的路由不是当前的ftp服务器走的路由。

             修改ftpclinent.java 中的代码实现。

            

protected Socket _openDataConnection_(String command, String arg) throws IOException {
		if (__dataConnectionMode != ACTIVE_LOCAL_DATA_CONNECTION_MODE
				&& __dataConnectionMode != PASSIVE_LOCAL_DATA_CONNECTION_MODE) {
			return null;
		}

		final boolean isInet6Address = getRemoteAddress() instanceof Inet6Address;

		Socket socket;

		if (__dataConnectionMode == ACTIVE_LOCAL_DATA_CONNECTION_MODE) {
			// if no activePortRange was set (correctly) -> getActivePort() = 0
			// -> new ServerSocket(0) -> bind to any free local port
			ServerSocket server = _serverSocketFactory_.createServerSocket(getActivePort(), 1, getHostAddress());

			try {
				// Try EPRT only if remote server is over IPv6, if not use PORT,
				// because EPRT has no advantage over PORT on IPv4.
				// It could even have the disadvantage,
				// that EPRT will make the data connection fail, because
				// today's intelligent NAT Firewalls are able to
				// substitute IP addresses in the PORT command,
				// but might not be able to recognize the EPRT command.
				if (isInet6Address) {
					if (!FTPReply.isPositiveCompletion(eprt(getReportHostAddress(), server.getLocalPort()))) {
						return null;
					}
				} else {
					if (!FTPReply.isPositiveCompletion(port(getReportHostAddress(), server.getLocalPort()))) {
						return null;
					}
				}

				if ((__restartOffset > 0) && !restart(__restartOffset)) {
					return null;
				}

				if (!FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
					return null;
				}

				// For now, let's just use the data timeout value for waiting for
				// the data connection.  It may be desirable to let this be a
				// separately configurable value.  In any case, we really want
				// to allow preventing the accept from blocking indefinitely.
				if (__dataTimeout >= 0) {
					server.setSoTimeout(__dataTimeout);
				}
				socket = server.accept();
			} finally {
				server.close();
			}
		} else { // We must be in PASSIVE_LOCAL_DATA_CONNECTION_MODE

			// Try EPSV command first on IPv6 - and IPv4 if enabled.
			// When using IPv4 with NAT it has the advantage
			// to work with more rare configurations.
			// E.g. if FTP server has a static PASV address (external network)
			// and the client is coming from another internal network.
			// In that case the data connection after PASV command would fail,
			// while EPSV would make the client succeed by taking just the port.
			boolean attemptEPSV = isUseEPSVwithIPv4() || isInet6Address;
			if (attemptEPSV && epsv() == FTPReply.ENTERING_EPSV_MODE) {
				_parseExtendedPassiveModeReply(_replyLines.get(0));
			} else {
				if (isInet6Address) {
					return null; // Must use EPSV for IPV6
				}
				// If EPSV failed on IPV4, revert to PASV
				if (pasv() != FTPReply.ENTERING_PASSIVE_MODE) {
					return null;
				}
				_parsePassiveModeReply(_replyLines.get(0));
			}

			socket = _socketFactory_.createSocket();
			// 新增加bind 源地址操作。
			if (localAddr != null) {
				System.out.println(">>>>>>>>>>>>>>>>>>FTPClient localAddr"+localAddr); 
				socket.bind(new InetSocketAddress(localAddr, localPort));
			}
			socket.connect(new InetSocketAddress(__passiveHost, __passivePort), connectTimeout);
			if ((__restartOffset > 0) && !restart(__restartOffset)) {
				socket.close();
				return null;
			}

			if (!FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
				socket.close();
				return null;
			}
		}

		if (__remoteVerificationEnabled && !verifyRemote(socket)) {
			socket.close();

			throw new IOException("Host attempting data connection " + socket.getInetAddress().getHostAddress()
					+ " is not same as server " + getRemoteAddress().getHostAddress());
		}

		if (__dataTimeout >= 0) {
			socket.setSoTimeout(__dataTimeout);
		}

		return socket;
	}

             

           


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apache FTPClient是一个开源的Java FTP客户端库,它可以用来连接FTP服务器并执行文件传输操作。Apache FTPClient连接FTP服务器的原理如下: 1. 创建FTPClient对象:首先,需要创建一个FTPClient对象来连接FTP服务器FTPClientApache FTPClient库提供的一个类,它提供了各种方法来连接FTP服务器、传输文件等。创建FTPClient对象的代码如下: ``` FTPClient ftpClient = new FTPClient(); ``` 2. 连接FTP服务器FTPClient对象创建后,需要使用connect方法连接FTP服务器。connect方法需要传入FTP服务器的地址和端口号。连接FTP服务器的代码如下: ``` ftpClient.connect(serverAddress, port); ``` 3. 登录FTP服务器:连接FTP服务器后,需要使用login方法登录FTP服务器。login方法需要传入FTP服务器的用户名和密码。登录FTP服务器的代码如下: ``` boolean success = ftpClient.login(username, password); ``` 4. 执行FTP操作:登录FTP服务器成功后,可以使用FTPClient对象提供的各种方法来执行FTP操作,比如上传文件、下载文件、删除文件等。FTP操作的代码如下: ``` ftpClient.changeWorkingDirectory(remoteDirectory); InputStream inputStream = new FileInputStream(localFile); ftpClient.storeFile(remoteFile, inputStream); inputStream.close(); ``` 5. 断开FTP连接:FTP操作完成后,需要使用disconnect方法断开FTP连接。断开FTP连接的代码如下: ``` ftpClient.disconnect(); ``` 以上就是Apache FTPClient连接FTP服务器的原理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值