当前位置: 代码迷 >> J2SE >> 为什么FTPClient listFiles() 返回的总是0,该怎么解决
  详细解决方案

为什么FTPClient listFiles() 返回的总是0,该怎么解决

热度:106   发布时间:2016-04-24 14:06:28.0
为什么FTPClient listFiles() 返回的总是0
我已经添加了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)


------解决方案--------------------
下面是我寫過關於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;