日期:2014-05-20 浏览次数:20990 次
/**
* 测试登录
* */
public boolean login(String serverIP, String user, String pwd) throws IOException {
boolean flag = false;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(serverIP);
if(ftpClient.login(user, pwd)){flag = true;}
}catch(IOException e){
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
}finally{
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
return flag;
}
System.out.println(new FTPService().login("192.168.1.101", "admin", "admin"));
true
System.out.println(new FTPService().login("192.168.56.1", "lab2", "admin"));
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:182)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:296)
at com.service.FTPService.login(FTPService.java:26)
at com.service.FTPService.main(FTPService.java:176)
Exception in thread "main" java.lang.RuntimeException: FTP客户端出错!
at com.service.FTPService.login(FTPService.java:30)
at com.service.FTPService.main(FTPService.java:176)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:182)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:296)
at com.service.FTPService.login(FTPService.java:26)
... 1 more