我已经添加了commons-net-1.4.1和jakarta-oro-2.0.8包。
测试过不同的ftp地址,有的listFiles()返回0有的没反映,谁知道为什么呀
谢谢!
以下程序:import org.apache.commons.net.*;
import org.apache.commons.net.ftp.*;public class ConnectServer 
{
    public ConnectServer(String server,String username, String password) 
    {
try 
{
    this.ftp = new FTPClient();
    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
    this.ftp.configure(conf);
    // Connect and logon to FTP Server
    this.ftp.connect(server);
    System.out.println("Connected to " + server + ".");
    this.ftp.login(username, password);
    System.out.print(this.ftp.getReplyString());
    //this.ftp.setFileType(FTP.BINARY_FILE_TYPE);
    //this.ftp.changeWorkingDirectory("/"); 不起任何作用
    FTPFile[] files = this.ftp.listFiles();
    System.out.println( "Number of files in dir: " + files.length );
    this.ftp.logout();
    System.out.print(this.ftp.getReplyString());
    this.ftp.disconnect();
    
}
catch (Exception e)
{
    System.out.println(e);
}
    }    public static void main (String args[])
    {
new ConnectServer("ftp.microsoft.com","anonymous", "anonymous");
    }
    private FTPClient ftp;
}运行结果:init:
deps-jar:
compile:
run-single:
Connected to ftp.microsoft.com.
230-Welcome to FTP.MICROSOFT.COM. Also visit http://www.microsoft.com/downloads.
230 Anonymous user logged in.
Number of files in dir: 0
221 Thank you for using Microsoft products.
BUILD SUCCESSFUL (total time: 4 seconds)

解决方案 »

  1.   

    下面是我寫過關於FTPClient,看對你有沒有一點幫助package com.XXXX;import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPFile;
    import org.apache.commons.net.ftp.FTPReply;import java.io.*;
    import java.net.SocketException;public class myFtpClient {
      protected FTPClient FTP_;
      protected String host;
      protected int port = 21;
      protected String userID;
      protected String password;  public myFtpClient() {
        FTP_ = new FTPClient();
      }  public myFtpClient(String host, String userID, String password) {
        this.host = host;
        this.userID = userID;
        this.password = password;
        FTP_ = new FTPClient();
      }  public myFtpClient(String host, int port, String userID, String password) {
        this.host = host;
        this.port = port;
        this.userID = userID;
        this.password = password;
        FTP_ = new FTPClient();
      }  /**
       * set Info
       *
       * @param host
       * @param port
       * @param userID
       * @param password
       */
      public void setInfo(String host, int port, String userID, String password) {
        this.host = host;
        this.port = port;
        this.userID = userID;
        this.password = password;
      }  /**
       * open connection
       *
       * @return boolean
       * @throws java.io.IOException
       * @throws SocketException
       */
      public boolean connect() throws Exception {
        FTP_.setDefaultPort(this.port);
        FTP_.setDataTimeout(120000);    //timeout為120秒
        FTP_.connect(this.host);
        if (!FTP_.isConnected()) {
          throw new Exception("NOT CONNECT FTP");
        }
        if (!FTPReply.isPositiveCompletion(FTP_.getReplyCode())) {
          FTP_.disconnect();
          System.out.println("Connection refused.");
          return false;
        }
        if (FTP_.login(this.userID, this.password)) {
          return true;
        } else {
          throw new Exception("NOT LOGIN");
        }
      }  /**
       * open connection by info
       *
       * @param host
       * @param userID
       * @param password
       * @return boolean
       * @throws IOException
       * @throws SocketException
       */
      public boolean connect(String host, String userID, String password)
          throws IOException, SocketException {
        try {
          FTP_.connect(host);
          if (!FTPReply.isPositiveCompletion(FTP_.getReplyCode())) {
            FTP_.disconnect();
            System.out.println("Connection refused.");
            return false;
          }
          FTP_.login(userID, password);
          return true;
        } catch (SocketException e) {
          throw e;
        } catch (IOException e) {
          throw e;
        }
      }
      

  2.   

    /**
       * close connection
       *
       * @throws IOException
       */
      public void disconnect() throws IOException {
        FTP_.logout();
        FTP_.disconnect();
      }  /**
       * retrieve one File from FTP
       *
       * @param remoteFileDir
       * @param localDir
       * @param remoteFileName
       * @return boolean
       */
      public boolean retrieveFile(String remoteFileDir, String localDir, String remoteFileName) throws Exception {
        FileOutputStream output = null;
        try {
          if (!FTP_.isConnected()) {
            System.out.println("connetion is closed.....");
            return false;
          }
          FTP_.changeWorkingDirectory(remoteFileDir);
          output = new FileOutputStream(localDir + "/" + remoteFileName);
          FTP_.retrieveFile(remoteFileName, output);
        } catch (Exception ex) {
          ex.printStackTrace();
          throw ex;
          //return false;
        } finally {
          if (output != null) {
            try {
              output.close();
            } catch (Exception e) {
            }
          }
        }
        return true;
      }  /**
       * retrieve one directory file from FTP
       *
       * @param remoteFileDir
       * @param localDir
       * @return boolean
       */
      public boolean retrieveAllFile(String remoteFileDir, String localDir) {
        FTPFile[] files = null;
        FileOutputStream output = null;
        try {
          if (!FTP_.isConnected()) {
            System.out.println("connetion is closed.....");
            return false;
          }      FTP_.changeWorkingDirectory(remoteFileDir);
          files = FTP_.listFiles();
          if (files != null) {
            for (int i = 0; i < files.length; i++) {
              if (files[i].isFile()) {
                output = new FileOutputStream(localDir + "/" + files[i].getName());
                FTP_.retrieveFile(files[i].getName(), output);
                output.flush();
              }
            }
          }
          return true;
        } catch (Exception ex) {
          ex.printStackTrace();
          return false;
        }
      }  /**
       * store one File to FTP
       *
       * @param remoteFileDir
       * @param localDir
       * @param localFileName
       * @return boolean
       */
      public boolean storeFile(String remoteFileDir, String localDir, String localFileName) {
        try {
          if (!FTP_.isConnected()) {
            System.out.println("connetion is closed.....");
            return false;
          }
          FTP_.changeWorkingDirectory(remoteFileDir);
          FileInputStream input = new FileInputStream(localDir + "/" + localFileName);
          FTP_.storeFile(localFileName, input);
          input.close();
        } catch (Exception ex) {
          ex.printStackTrace();
          return false;
        }
        return true;
      }  /**
       * store one Directory File to FTP
       *
       * @param remoteFileDir
       * @param localDir
       * @return boolean
       */
      public boolean storeAllFile(String remoteFileDir, String localDir) {
        File ofile = new File(localDir);
        String[] sFileName = ofile.list();
        FileInputStream input = null;
        try {
          if (!FTP_.isConnected()) {
            System.out.println("connetion is closed.....");
            return false;
          }
          FTP_.changeWorkingDirectory(remoteFileDir);
          for (int i = 0; i < sFileName.length; i++) {
            input = new FileInputStream(localDir + "/" + sFileName[i]);
            FTP_.storeFile(sFileName[i], input);
          }
        } catch (Exception ex) {
          ex.printStackTrace();
          return false;
        }
        return true;
      }  /**
       * delete current directory FTP file
       *
       * @param pathname
       * @return b
       */
      public boolean deleteFile(String pathname) {
        try {
          if (!FTP_.isConnected()) {
            System.out.println("connetion is closed.....");
            return false;
          }
          FTP_.deleteFile(pathname);
        } catch (IOException ex) {
          ex.printStackTrace();
          return false;
        }
        return true;
      }  /**
       * delete designate directory FTP file
       *
       * @param remoteFileDir
       * @param pathname
       * @return b
       */
      public boolean deleteDirFile(String remoteFileDir, String pathname) {
        try {
          if (!FTP_.isConnected()) {
            System.out.println("connetion is closed.....");
            return false;
          }
          FTP_.changeWorkingDirectory(remoteFileDir);
          FTP_.deleteFile(pathname);
        } catch (IOException ex) {
          ex.printStackTrace();
          return false;
        }
        return true;
      }  /**
       * delete all designate directory FTP file
       *
       * @param remoteFileDir
       * @return b
       */
      public boolean deleteAllFile(String remoteFileDir) {
        FTPFile[] files = null;
        try {
          if (!FTP_.isConnected()) {
            System.out.println("connetion is closed.....");
            return false;
          }
          FTP_.changeWorkingDirectory(remoteFileDir);
          files = FTP_.listFiles();
          if (files != null) {
            for (int i = 0; i < files.length; i++) {
              if (files[i].isFile()) {
                FTP_.deleteFile(files[i].getName());
              }
            }
          }
        } catch (IOException ex) {
          ex.printStackTrace();
          return false;
        }
        return true;
      }  public boolean changeWorkDir(String dir) {
        try {
          return FTP_.changeWorkingDirectory(dir);
        } catch (IOException e) {
          return false;
        }
      }  /**
       * Resume file
       *
       * @param remoteFileDir
       * @param localDir
       * @param remoteFileName
       * @return b
       */
      public boolean fileTransferResume(String remoteFileDir, String localDir, String remoteFileName) {    if (!FTP_.isConnected()) {
          System.out.println("connetion is closed.....");
          return false;
        }
        File file = new File(localDir + "/" + remoteFileName);
        RandomAccessFile rafile = null;
        InputStream datStream = null;
        byte[] buf = new byte[20480];
        try {
          rafile = new RandomAccessFile(file, "rw");
          long length = rafile.length();
          rafile.seek(length);      datStream = FTP_.retrieveFileStream(remoteFileName);      int read = -1;
          do {
            read = datStream.read(buf);
            if (read != -1)
              rafile.write(buf, 0, read);
          } while (read != -1);
          rafile.close();
          return true;    } catch (Exception e) {
          e.printStackTrace();
        }    return true;
      }  public FTPFile[] listFiles() throws IOException {
        return FTP_.listFiles();
      }  public FTPClient getFTP_() {
        return FTP_;
      }  public void setFTP_(FTPClient connect) {
        FTP_ = connect;
      }  public String getHost() {
        return host;
      }  public void setHost(String host) {
        this.host = host;
      }  public String getPassword() {
        return password;
      }  public void setPassword(String password) {
        this.password = password;
      }  public int getPort() {
        return port;
      }  public void setPort(int port) {
        this.port = port;
      }  public String getUserID() {
        return userID;
      }  public void setUserID(String userID) {
        this.userID = userID;
      }}