3

我想使用 Java 连接到 FTP 服务器,在本例中为 FileZilla 服务器。我已经下载了 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

谁能帮我?我真的不知道为什么会这样。

4

1 回答 1

0

当客户端尝试打开侦听套接字以供服务器连接并发送目录列表时,就会发生错误。我不确定为什么会发生这种情况,但我建议将连接模式从“主动”更改为“被动”,这可能会绕过问题。你可以这样做:

ftp.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.PASV);
于 2016-09-01T05:22:15.767 回答