FTP サーバー (この場合は FileZilla サーバー) に Java で接続したいと考えています。私はedtFTPJ/free をダウンロードし、そのパッケージで送られてきた例を試してきました。サーバーへの接続、フォルダー/ファイルの削除、名前の変更、フォルダーの作成は機能しますが、ディレクトリ リストを取得したいときに接続が切断されます。(これはFTP4Jと呼ばれる別のライブラリでも発生します。) コードは次のとおりです。
package ftp_classes;
import com.enterprisedt.net.ftp.FileTransferClient;
import com.enterprisedt.net.ftp.FTPFile;
import com.enterprisedt.util.debug.Level;
import com.enterprisedt.util.debug.Logger;
public class GetDirectoryListing {
public static void main(String[] args) {
String host = "localhost";
String username = "user";
String password = "password";
// set up logger so that we get some output
Logger log = Logger.getLogger(GetDirectoryListing.class);
Logger.setLevel(Level.INFO);
FileTransferClient ftp = null;
try {
com.enterprisedt.util.debug.Logger.setLevel(com.enterprisedt.util.debug.Level.DEBUG);
// create client
log.info("Creating FTP client");
ftp = new FileTransferClient();
// set remote host
log.info("Setting remote host");
ftp.setRemoteHost(host);
ftp.setUserName(username);
ftp.setPassword(password);
// connect to the server
log.info("Connecting to server " + host);
ftp.connect();
log.info("Connected and logged in to server " + host);
log.info("Getting current directory listing");
FTPFile[] files = ftp.directoryList(".");
for (int i = 0; i < files.length; i++) {
log.info(files[i].toString());
}
// Shut down client
log.info("Quitting client");
ftp.disconnect();
log.info("Example complete");
} catch (Exception e) {
e.printStackTrace();
}
}
}
コンソール出力は次のとおりです。
INFO [ftp_classes.GetDirectoryListing] 1 Jun 2012 14:05:00.151 : Setting remote host
INFO [ftp_classes.GetDirectoryListing] 1 Jun 2012 14:05:00.151 : Connecting to server localhost
DEBUG [FileTransferClient] 1 Jun 2012 14:05:00.151 : Configured client
DEBUG [FTPClient] 1 Jun 2012 14:05:00.159 : Connecting to localhost/127.0.0.1:21
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.175 : 220-FileZilla Server version 0.9.41 beta
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.175 : 220-written by Tim Kosse (Tim.Kosse@gmx.de)
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.177 : 220 Please visit http://sourceforge.net/projects/filezilla/
DEBUG [FileTransferClient] 1 Jun 2012 14:05:00.179 : Client connected
DEBUG [FileTransferClient] 1 Jun 2012 14:05:00.180 : Logging in
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.180 : ---> USER user
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.180 : 331 Password required for user
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.181 : ---> PASS ********
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.181 : 230 Logged on
DEBUG [FileTransferClient] 1 Jun 2012 14:05:00.181 : Logged in
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.181 : ---> TYPE I
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.182 : 200 Type set to I
INFO [ftp_classes.GetDirectoryListing] 1 Jun 2012 14:05:00.182 : Connected and logged in to server localhost
INFO [ftp_classes.GetDirectoryListing] 1 Jun 2012 14:05:00.182 : Getting current directory listing
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.189 : ---> SYST
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.189 : 215 UNIX emulated by FileZilla
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.194 : ---> PWD
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.194 : 257 "/" is current directory.
DEBUG [FTPClient] 1 Jun 2012 14:05:00.194 : setupDirDetails(.) returning: /
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.195 : ListenOnAllInterfaces=true
com.enterprisedt.net.ftp.ControlChannelIOException: Software caused connection abort: socket write error
at com.enterprisedt.net.ftp.FTPControlSocket.writeCommand(FTPControlSocket.java:1020)
at com.enterprisedt.net.ftp.FTPControlSocket.sendCommand(FTPControlSocket.java:997)
at com.enterprisedt.net.ftp.FTPControlSocket.setDataPort(FTPControlSocket.java:813)
at com.enterprisedt.net.ftp.FTPControlSocket.sendPORTCommand(FTPControlSocket.java:669)
at com.enterprisedt.net.ftp.FTPControlSocket.createDataSocketActive(FTPControlSocket.java:616)
at com.enterprisedt.net.ftp.FTPControlSocket.createDataSocket(FTPControlSocket.java:583)
at com.enterprisedt.net.ftp.FTPClient.setupDataSocket(FTPClient.java:2648)
at com.enterprisedt.net.ftp.FTPClient.dir(FTPClient.java:3664)
at com.enterprisedt.net.ftp.FTPClient.dir(FTPClient.java:3756)
at com.enterprisedt.net.ftp.FTPClient.dirDetails(FTPClient.java:3583)
at com.enterprisedt.net.ftp.FileTransferClient.directoryList(FileTransferClient.java:647)
at ftp_classes.GetDirectoryListing.main(GetDirectoryListing.java:52)
DEBUG [FTPControlSocket] 1 Jun 2012 14:05:00.197 : ---> PORT 127,0,0,1,201,168
誰でも私を助けることができますか?なぜこれが起こっているのか本当にわかりません。