今天有需要做一个FTP 的客户端,下载服务器的文件夹内的所有文件,一个一个的用get 下载显然不现实、在网上收到了common- net 。于是利用这个jar 包,提供的ftp 实现来下载。但是仿造网上别人的Demo 来写了一个。就是在 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"); } } |
|
100分 |
楼主参考一下http://blog.csdn.net/java2000_net/article/details/3718852,这个博主的文章都很靠谱的
|
算了,现在也没空继续研究这个了,每人回答,分全给你吧
|
|
我也遇见这个问题,好像说是不支持中文版ftp服务器的原因,但是目前也一直无法解决。
|