`
xrqsjj
  • 浏览: 20508 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

ftp 连接

    博客分类:
  • j2se
阅读更多
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");
        }
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics