读书人

为什么FTPClient listFiles() 返回的总

发布时间: 2012-01-16 23:36:51 作者: rapoo

为什么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;


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); //timeout120秒
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;
}
}

读书人网 >J2SE开发

热点推荐