getPath(ftp,path,pathArray);
public static void getPath(FTPClient ftp,String path,ArrayList<String> pathArray) throws IOException{
FTPFile[] files = ftp.listFiles();
for (FTPFile ftpFile : files) {
if(ftpFile.getName().equals(".")||ftpFile.getName().equals(".."))continue;
if(ftpFile.isDirectory()){//如果是目录,则递归调用,查找里面所有文件
path+="/"+ftpFile.getName();
pathArray.add(path);
ftp.changeWorkingDirectory(path);//改变当前路径
getPath(ftp,path,pathArray);//递归调用
path=path.substring(0, path.lastIndexOf("/"));//避免对之后的同目录下的路径构造作出干扰,
}
}
}
public static void download(FTPClient ftp,ArrayList<String> pathArray,String localRootPath) throws IOException{
for (String string : pathArray) {
String localPath=localRootPath+string;
File localFile = new File(localPath);
if (!localFile.exists()) {
localFile.mkdirs();
}
}
for (String string : pathArray) {
String localPath=localRootPath+string;//构造本地路径
ftp.changeWorkingDirectory(string);
FTPFile[] file=ftp.listFiles();
for (FTPFile ftpFile : file) {
if(ftpFile.getName().equals(".")||ftpFile.getName().equals(".."))continue;
File localFile = new File(localPath);
if(!ftpFile.isDirectory()){
OutputStream is = new FileOutputStream(localFile+"/"+ftpFile.getName());
ftp.retrieveFile(ftpFile.getName(), is);
is.close();
}
}
}
}
public static void main(String[] args) throws SocketException, IOException {
FTPClient ftp = new FTPClient();
ftp.connect("127.0.0.1");
ftp.login("test","test");
int reply;
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
ftp.setBufferSize(1024);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
String path="";
ArrayList<String> pathArray=new ArrayList<String>();
getPath(ftp,path,pathArray);
System.out.println(pathArray);
download(ftp, pathArray, "c:\\download");
ftp.logout();
ftp.disconnect();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有