// parenthesis are optionals and we have to use 3 groups.
Pattern p = Pattern.compile("227 .* \\(?(\\d{1,3},\\d{1,3},\\d{1,3},\\d{1,3}),(\\d{1,3}),(\\d{1,3})\\)?");
Matcher m = p.matcher(serverAnswer);
if (!m.find()) {
throw new NXProtocolException("PASV failed : " + serverAnswer);
}
// Get port number out of group 2 & 3
port = Integer.parseInt(m.group(3)) + (Integer.parseInt(m.group(2)) << 8);
// IP address is simple
String s = m.group(1).replace(',', '.');
dest = new InetSocketAddress(s, port);
} else if (issueCommand("EPSV ALL") == FTP_SUCCESS) {
// We can safely use EPSV commands
if (issueCommand("EPSV") == FTP_ERROR) {
throw new NXProtocolException("EPSV Failed: " + getResponseString());
}
serverAnswer = getResponseString();
// The response string from a EPSV command will contain the port number
// the format will be :
// 229 Entering Extended Passive Mode (|||58210|)
//
// So we'll use the regular expresions package to parse the output.
Pattern p = Pattern.compile("^229 .* \\(\\|\\|\\|(\\d+)\\|\\)");
Matcher m = p.matcher(serverAnswer);
if (!m.find()) {
throw new NXProtocolException("EPSV failed : " + serverAnswer);
}
// Yay! Let's extract the port number
String s = m.group(1);
port = Integer.parseInt(s);
InetAddress add = serverSocket.getInetAddress();