用 common-net ftpclient 访问目录时,ftpclient.listFiles长度是0

J2EE 码拜 9年前 (2015-04-05) 1919次浏览 0个评论

今天有需要做一个FTP 的客户端,下载服务器的文件夹内的所有文件,一个一个的用get 下载显然不现实、在网上收到了common- net 。于是利用这个jar 包,提供的ftp 实现来下载。但是仿造网上别人的Demo 来写了一个。就是在
ftpclient.listFiles()  这个方法执行时就是没有返回数据( 长度始终为0 )。尽管文件夹内有文件。
但是ftpclient.listName(); 确是能获取到文件内的文件名。
服务器 : winserver 2003
客户端 : xp
commons-net : commons-net-3.3.jar
ps:  尽管网上说由于时间中包含中文的问题可能引起这样的问题,或者在listFiles 时要先进入被动模式。但都是针对的是Linux 服务器来说的。上面两种都尝试了,  listFiles()   返回数组长度还是0

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;

import com.ftphelp.exception.FileTransmissionException;
import com.ftphelp.exception.FtpClientLaunchException;
import com.ftphelp.exception.IOErrorException;
import com.ftphelp.exception.InitFtpClientException;

public class FtpDirGetHelp {
	private String hostname;
	private String ftpUser;
	private String ftpPassWord;
	private String localBaseDir;
	private String remoteBaseDir;

	private static FTPClient ftpClient=new FTPClient();

	public FtpDirGetHelp(String hostname,
			String ftpUser,
			String ftpPassWord,
			String localBaseDir,
			String remoteBaseDir){
		this.hostname=hostname;
		this.ftpUser=ftpUser;
		this.ftpPassWord=ftpPassWord;
		this.localBaseDir=localBaseDir;
		this.remoteBaseDir=remoteBaseDir;
	}

	public FtpDirGetHelp(){

	}

	protected void getDirOrFile(FTPFile ftpFile,
			String relativeLocalPath,
			String relativeRemotePath){
		if(ftpFile.isFile()){
			//如果文件名没有乱码
			System.out.println("\t下载文件-"+ftpFile.getName());
			if(ftpFile.getName().indexOf("?")==-1)
			{
				OutputStream outputStream=null;

				try
				{
					//指定输出处
					outputStream=new FileOutputStream(relativeLocalPath+ftpFile.getName());
					//ftp 往输出处推送数据
					ftpClient.enterLocalPassiveMode();
					ftpClient.retrieveFile(ftpFile.getName(), outputStream);
					outputStream.flush();
					outputStream.close();
				}catch(Exception e){
					throw new FileTransmissionException("文件传输出错",e);
				}finally
				{
					if(outputStream!=null)
					{
						try
						{
							outputStream.close();
						}catch(IOException e){
							throw new IOErrorException("输出流无法关闭",e);
						}
					}
				}

			}
		}else{//处理文件夹
			System.out.println("打开文件夹"+ftpFile.getName());
			String newLocalRelatePath=relativeLocalPath+ftpFile.getName();
			String newRemotePath=relativeRemotePath+ftpFile.getName();

			File file=new File(newLocalRelatePath);
			if(!file.exists())
			{
				file.mkdirs();
			}
			try
			{
				newLocalRelatePath+="/";
				newRemotePath+="/";

				boolean changedir=ftpClient.changeWorkingDirectory(newRemotePath);
				if(changedir){
					if(!isContainerNoUsefulDir(ftpFile.getName())){
						FTPFile[] files=ftpClient.listFiles();
						for(FTPFile netedFtpFile:files){
							this.getDirOrFile(netedFtpFile, newLocalRelatePath, newRemotePath);
						}
					}
					ftpClient.changeToParentDirectory();
				}
			}catch(Exception e)
			{
				System.err.println("ftp 文件夹处理出错");
			}
		}
	}

	public boolean isContainerNoUsefulDir(String currentDir){
		List<String> noUsefulDirs=new ArrayList(){
			{
				this.add("VT");
				this.add("DS");
			}
		}; 

		for(String nousefulldir:noUsefulDirs){
			if(nousefulldir.equals(currentDir))
				return true;
		}
		return false;
	}

	public void download(){
		try{
			FTPClientConfig conf=new FTPClientConfig(FTPClientConfig.SYST_NT);
			ftpClient.configure(conf);
			ftpClient.setControlEncoding("GBK");
			ftpClient.connect(hostname);
			boolean isLoginSuccess= ftpClient.login(ftpUser, ftpPassWord);
			System.out.println("FTP 登录是否成功   "+isLoginSuccess);
		}catch(Exception e){
			//System.err.println("FTP 客户端初始化有误");
			throw new InitFtpClientException("FTP 客户端初始化有误",e);
		}

		try{
			FTPFile[] firstRangFiles=null;
			boolean changedir=ftpClient.changeWorkingDirectory(remoteBaseDir);
			System.out.println("首目录是否被认出 "+changedir);
			if(changedir){
				//System.out.println(ftpClient.sendCommand("cd", "/C:/"));

				//============下面ftpClient.listFiles() 返回数组死活都是  0
				ftpClient.enterLocalPassiveMode();
				firstRangFiles=ftpClient.listFiles();
//				for(String name:ftpClient.listNames()){
//					System.out.println(name);
//				}

				for(FTPFile firstRangFtpFile:firstRangFiles){
					System.out.println(firstRangFtpFile.getName()+"\t是否是文件夹-"+firstRangFtpFile.isDirectory());
					//this.getDirOrFile(firstRangFtpFile, localBaseDir, remoteBaseDir);
				}
			}
			System.out.println("FTP 执行完毕");
		}catch(Exception e){
			throw new FtpClientLaunchException("FTP 下载启动失败",e);
		}

	}
       public static void main(String[] args){
		//访问
		System.out.println("start");
		String remotehost="117.117.117.117";
		String name="A";
		String pwd="B";
		String localFtpDir="C:\ftpGet\";
		String remoteFtpDir="/tool/";
		try{
		new FtpDirGetHelp(remotehost,name,pwd,localFtpDir,remoteFtpDir).download();
		}catch(Exception e){
			System.err.println(e.getMessage());
			e.printStackTrace();
		}
		System.out.println("end");
	}

}
用 common-net ftpclient 访问目录时,ftpclient.listFiles长度是0
100分
楼主参考一下http://blog.csdn.net/java2000_net/article/details/3718852,这个博主的文章都很靠谱的
用 common-net ftpclient 访问目录时,ftpclient.listFiles长度是0
算了,现在也没空继续研究这个了,每人回答,分全给你吧
用 common-net ftpclient 访问目录时,ftpclient.listFiles长度是0
我也遇见这个问题,好像说是不支持中文版ftp服务器的原因,但是目前也一直无法解决。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明用 common-net ftpclient 访问目录时,ftpclient.listFiles长度是0
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!