1.直接的socket连接,改自openNMS,参考ftp://ftp.rfc-editor.org/in-notes/std/std9.txt
2.运用开发包API,edtftpj-1.5.3, http://www.enterprisedt.com
int
retry
=
2
;
int port = 21 ;
int timeout = 3000 ;
String userid = " test " ;
String password = " test " ;
InetAddress ipv4Addr = null ;
try {
ipv4Addr = InetAddress.getByName("192.168.7.61");
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
int serviceStatus = 1 ;
long responseTime = - 1 ;
for ( int attempts = 0 ; attempts <= retry && serviceStatus == 1 ; attempts ++ ) {
Socket socket = null;
try {
// create a connected socket
long sentTime = System.currentTimeMillis();
socket = new Socket();
socket.connect(new InetSocketAddress(ipv4Addr, port), timeout);
socket.setSoTimeout(timeout);
// We're connected, so upgrade status to unresponsive
serviceStatus = -1;
BufferedReader lineRdr = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
// Tokenize the Banner Line, and check the first
// line for a valid return.
responseTime = System.currentTimeMillis() - sentTime;
String banner = lineRdr.readLine();
System.out.println("banner=" + banner);
StringTokenizer t = new StringTokenizer(banner);
int rc = -1;
try {
rc = Integer.parseInt(t.nextToken());
} catch (NumberFormatException nfE) {
nfE.fillInStackTrace();
}
// Verify that return code is in proper range.
if (rc >= 200 && rc <= 299) {
// Attempt to login if userid and password available
boolean bLoginOk = false;
if (userid == null || userid.length() == 0
|| password == null || password.length() == 0) {
bLoginOk = true;
} else {
// send the use string
String cmd = "user " + userid + " ";
socket.getOutputStream().write(cmd.getBytes());
// get the response code.
String response = null;
response = lineRdr.readLine();
System.out.println("response=" + response);
t = new StringTokenizer(response);
rc = Integer.parseInt(t.nextToken());
// Verify that return code is in proper range.
if (rc >= 200 && rc <= 399) {
// send the password
cmd = "pass " + password + " ";
socket.getOutputStream().write(cmd.getBytes());
// get the response...check for multi-line response
response = lineRdr.readLine();
if (response == null)
continue;
// Verify that return code is in proper range.
System.out.println("FtpMonitor.poll: tokenizing respone to check for return code: "+ response);
t = new StringTokenizer(response);
rc = Integer.parseInt(t.nextToken());
if (rc >= 200 && rc <= 299) {
System.out.println("FtpMonitor.poll: Login successful, parsed return code: "+ rc);
bLoginOk = true;
} else {
System.out.println("FtpMonitor.poll: Login failed, parsed return code: "+ rc);
bLoginOk = false;
}
}
}
if (bLoginOk) {
// FTP should recognize the QUIT command
String cmd = "QUIT ";
socket.getOutputStream().write(cmd.getBytes());
// get the returned string, tokenize, and
// verify the correct output.
String response = lineRdr.readLine();
t = new StringTokenizer(response);
rc = Integer.parseInt(t.nextToken());
// Verify that return code is in proper range.
System.out.println("rc==" + rc);
serviceStatus = 1;
}
}
// If we get this far and the status has not been set
// to available, then something didn't verify during
// the banner checking or login/QUIT command process.
if (serviceStatus == 1) {
serviceStatus = -1;
}
} catch (Exception e) {
e.printStackTrace();
}
}
int port = 21 ;
int timeout = 3000 ;
String userid = " test " ;
String password = " test " ;
InetAddress ipv4Addr = null ;
try {
ipv4Addr = InetAddress.getByName("192.168.7.61");
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
int serviceStatus = 1 ;
long responseTime = - 1 ;
for ( int attempts = 0 ; attempts <= retry && serviceStatus == 1 ; attempts ++ ) {
Socket socket = null;
try {
// create a connected socket
long sentTime = System.currentTimeMillis();
socket = new Socket();
socket.connect(new InetSocketAddress(ipv4Addr, port), timeout);
socket.setSoTimeout(timeout);
// We're connected, so upgrade status to unresponsive
serviceStatus = -1;
BufferedReader lineRdr = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
// Tokenize the Banner Line, and check the first
// line for a valid return.
responseTime = System.currentTimeMillis() - sentTime;
String banner = lineRdr.readLine();
System.out.println("banner=" + banner);
StringTokenizer t = new StringTokenizer(banner);
int rc = -1;
try {
rc = Integer.parseInt(t.nextToken());
} catch (NumberFormatException nfE) {
nfE.fillInStackTrace();
}
// Verify that return code is in proper range.
if (rc >= 200 && rc <= 299) {
// Attempt to login if userid and password available
boolean bLoginOk = false;
if (userid == null || userid.length() == 0
|| password == null || password.length() == 0) {
bLoginOk = true;
} else {
// send the use string
String cmd = "user " + userid + " ";
socket.getOutputStream().write(cmd.getBytes());
// get the response code.
String response = null;
response = lineRdr.readLine();
System.out.println("response=" + response);
t = new StringTokenizer(response);
rc = Integer.parseInt(t.nextToken());
// Verify that return code is in proper range.
if (rc >= 200 && rc <= 399) {
// send the password
cmd = "pass " + password + " ";
socket.getOutputStream().write(cmd.getBytes());
// get the response...check for multi-line response
response = lineRdr.readLine();
if (response == null)
continue;
// Verify that return code is in proper range.
System.out.println("FtpMonitor.poll: tokenizing respone to check for return code: "+ response);
t = new StringTokenizer(response);
rc = Integer.parseInt(t.nextToken());
if (rc >= 200 && rc <= 299) {
System.out.println("FtpMonitor.poll: Login successful, parsed return code: "+ rc);
bLoginOk = true;
} else {
System.out.println("FtpMonitor.poll: Login failed, parsed return code: "+ rc);
bLoginOk = false;
}
}
}
if (bLoginOk) {
// FTP should recognize the QUIT command
String cmd = "QUIT ";
socket.getOutputStream().write(cmd.getBytes());
// get the returned string, tokenize, and
// verify the correct output.
String response = lineRdr.readLine();
t = new StringTokenizer(response);
rc = Integer.parseInt(t.nextToken());
// Verify that return code is in proper range.
System.out.println("rc==" + rc);
serviceStatus = 1;
}
}
// If we get this far and the status has not been set
// to available, then something didn't verify during
// the banner checking or login/QUIT command process.
if (serviceStatus == 1) {
serviceStatus = -1;
}
} catch (Exception e) {
e.printStackTrace();
}
}
2.运用开发包API,edtftpj-1.5.3, http://www.enterprisedt.com
FTPClient ftp
=
new
FTPClient();
try {
ftp.setRemoteHost("192.168.7.61");
ftp.setRemotePort(21);
ftp.setConnectMode(FTPConnectMode.ACTIVE);
ftp.connect();
ftp.login("test", "test");
} catch (Exception e) {
System.out.println("connect error");
e.printStackTrace();
} finally {
if (ftp != null)
try {
ftp.quit();
} catch (Exception e) {
System.out.println("quit error");
e.printStackTrace();
}
}
try {
ftp.setRemoteHost("192.168.7.61");
ftp.setRemotePort(21);
ftp.setConnectMode(FTPConnectMode.ACTIVE);
ftp.connect();
ftp.login("test", "test");
} catch (Exception e) {
System.out.println("connect error");
e.printStackTrace();
} finally {
if (ftp != null)
try {
ftp.quit();
} catch (Exception e) {
System.out.println("quit error");
e.printStackTrace();
}
}