读书人

ftp 联接

发布时间: 2012-12-19 14:13:14 作者: rapoo

ftp 连接

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");        }    }}

读书人网 >编程

热点推荐