(转)重写common-net-ftp以适应内网Passive模式

转自[url]http://www.diybl.com/course/3_program/java/javajs/20100719/453294.html[/url]
小心弹出窗口Ad

需要重写几个地方
i. org.apache.commons.net.SocketClient

/** 用来自定义 Passive 模式的 Host */
protected String _set_passive_host = "127.0.0.1";


/**
* Opens a Socket connected to a remote host at the specified port and
* originating from the current host at a system assigned port.
* Before returning, {@link #_connectAction_ _connectAction_() }
* is called to perform connection initialization actions.
*


* @param hostname The name of the remote host.
* @param port The port to connect to on the remote host.
* @exception SocketException If the socket timeout could not be set.
* @exception IOException If the socket could not be opened. In most
* cases you will only want to catch IOException since SocketException is
* derived from it.
* @exception UnknownHostException If the hostname cannot be resolved.
*/
public void connect(String hostname, int port)
throws SocketException, IOException
{
// 将连接的hostname赋给_set_passive_host
_set_passive_host = InetAddress.getByName(hostname).getHostAddress();
_socket_= _socketFactory_.createSocket();
_socket_.connect(new InetSocketAddress(hostname, port), connectTimeout);

_connectAction_();
}



ii. org.apache.commons.net.ftp.FTPClient

private void __parsePassiveModeReply(String reply)
throws MalformedServerReplyException
{
java.util.regex.Matcher m = __parms_pat.matcher(reply);
if (!m.find()) {
throw new MalformedServerReplyException(
"Could not parse passive host information.\nServer Reply: " + reply);
}
reply = m.group();
String parts[] = m.group().split(",");

//__passiveHost = parts[0] + '.' + parts[1] + '.' + parts[2] + '.' + parts[3];
// 此处将_set_passive_host赋给__passiveHost
__passiveHost = _set_passive_host;
System.out.println(">----------->> Real Host IP is : "+__passiveHost);
try
{
int oct1 = Integer.parseInt(parts[4]);
int oct2 = Integer.parseInt(parts[5]);
__passivePort = (oct1 << 8) | oct2;
}
catch (NumberFormatException e)
{
throw new MalformedServerReplyException(
"Could not parse passive host information.\nServer Reply: " + reply);
}

}

你可能感兴趣的:(技术备忘)