- 论坛徽章:
- 0
|
本帖最后由 @zheng 于 2010-06-21 17:03 编辑
- import java.io.*;
- import java.net.*;
- import sun.net.ftp.FtpClient;
- class CustomFtpClient extends FtpClient {
- public CustomFtpClient(String host) throws IOException {
- super(host);
- }
- public CustomFtpClient(String host, int port) throws IOException {
- super(host, port);
- }
- public CustomFtpClient() {
- super();
- }
- public void setTimeout(int timeout) {
- if (serverSocket != null) {
- try {
- serverSocket.setSoTimeout(timeout);
- } catch (SocketException socketException) {
- }
- }
- }
- }
- public class Test {
- public static void main(String[] args) {
- String host = "127.0.0.1";
- String user = "username";
- String password = "password";
- try {
- CustomFtpClient ftpClient = new CustomFtpClient();
- ftpClient.openServer(host);
- ftpClient.setTimeout(5000); // set timeout in 5 seconds
- ftpClient.login(user, password);
- ftpClient.binary();
- ftpClient.closeServer();
- System.out.println("Connect is OK");
- } catch (Exception exp) {
- exp.printStackTrace();
- System.out.println("Connect is Timeout");
- }
- }
- }
复制代码 |
|