ページ更新: 2004-08-14 (土) (5149日前)

関連:

2004-08-13

Jakarta Commons Net

2005-06-15時点の最新は 1.5.0。

目次

[編集]

情報源 #

Jakarta Commons/Net - Jakarta Commons Net
http://jakarta.apache.org/commons/net/

depends on

Jakarta ORO - Jakarta ORO
http://jakarta.apache.org/oro/

Jakarta Commonsによるネットワークプログラミング (Java Sticky Note)

[編集]

ftp #

TryFtp.java

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory;

public class TryFtp {

    static String FTP_SERVER = "192.168.1.2";
    static String FTP_USER = "username";
    static String FTP_PASSWD = "XXXXXXXX";

    public static void main(String[] args) throws Exception {
        ftpTest(FTP_SERVER, FTP_USER, FTP_PASSWD);
    }

    public static void ftpTest(String server, String ftpuser, String password) throws Exception {
        FTPClient client = new FTPClient();
        try {
            // settings.
            client.setDataTimeout(10*1000);
            client.setDefaultTimeout(10*1000);
            client.setParserFactory(new PrivateFTPFileEntryParserFactory());
            
            //
            client.connect(server);
            System.out.println("Connected to " + server + ".");
            System.out.print(client.getReplyString());
            int reply = client.getReplyCode();
            System.out.print(reply);

            //            
            if(!FTPReply.isPositiveCompletion(reply)) {
                client.disconnect();
                return;
            }
            
            client.login(ftpuser, password);

            String[] names = client.listNames();

            FTPFile[] list = client.listFiles();
        } finally {
            if (client != null && client.isConnected()) {
                client.logout();
                client.disconnect();
            }
        }
    }

    static class PrivateFTPFileEntryParserFactory extends DefaultFTPFileEntryParserFactory {
    }

}

Packetyzer 3.0.1 でキャプチャ、'Follow TCP Flow'、Decode タブ選択。

ftpサーバは 192.168.1.2、ProFTPD 1.2.9 (Debian GNU/Linux Sarge)、クライアントは 192.168.1.4。

220 ProFTPD 1.2.9 Server (ftpserver) [ftpserver]   ★ FTPClient#connect
USER username                                      ★ FTPClient#login
......331 Password required for username.
PASS XXXXXXXX
230 User username logged in.
PORT 192,168,1,4,7,91                              ★ FTPClient#listNames()
200 PORT command successful
NLST
150 Opening ASCII mode data connection for file list
226 Transfer complete.
SYST                                               ★ FTPClient#listFiles()
215 UNIX Type: L8
PORT 192,168,1,4,7,92
200 PORT command successful
LIST
150 Opening ASCII mode data connection for file list
226 Transfer complete.
QUIT                                               ★ FTPClient#logout()
221 Goodbye.

ちなみに、ffftp 1.91 だとこうなる。(一部抜粋)

XPWD
257 "/" is current directory.
TYPE A
200 Type set to A
PORT 127.0.0.1,9,198
200 PORT command successful
NLST -alL                      ここで、ファイル一覧を取得
150 Opening ASCII mode data connection for file list
226 Transfer complete.
TYPE I
200 Type set to I
PORT 127.0.0.1,9,199
200 PORT command successful
RETR /sample.txt                ここで、ファイルを取得
150 Opening BINARY mode data connection for /sample.txt (46716 bytes)
226 Transfer complete.