java ftpclient 递归下载问题

public void getGaojingData(QueryCriteria criteria) {
//切换GP数据库
CustomerContextHolder.setContextType(CustomerContextHolder.SESSION_FACTORY_GP);
FtpUtil ftpUtil = new FtpUtil();
//连接ftp
FTPClient ftpClient = ftpUtil.connect();
try {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // 设置文件类型(二进制)
ftpClient.enterLocalPassiveMode();
String remotePath ="/FMS_ALARMDATA/";
if(ftpUtil.changeDirectory(remotePath)){//如果切换目录成功
//获取 remotePath 目录下所有文件夹
FTPFile[] fs2 = ftpClient.listFiles();
for(int i =0;i //判断是文件夹还是文件,返回完整路径路径、例如 /FMS_ALARMDATA/GZ/20161011/czxxx.csv.gz
String remotePath1 ="/"+ fs2[i].getName(); ///FMS_ALARMDATA/GZ
//进入下一层目录FMS_ALARMDATA/GZ
ftpUtil.changeDirectory(remotePath1+"/");
FTPFile[] fsCity = ftpClient.listFiles();///FMS_ALARMDATA/GZ
for (int j=0;j String remotePath2 ="/"+fsCity[j].getName()+"/"; ///FMS_ALARMDATA/GZ/20160912
ftpUtil.changeDirectory(remotePath2);
FTPFile[] fsFile = ftpClient.listFiles();
for(int r=0;r //适配程序修改名称
String fileName = new StringBuffer(fsFile[r].getName()).replace(13,14,"-").toString().toLowerCase();
criteria.put("fileName",fileName);
criteria.put("fileTime",fsFile[r].getTimestamp().getTime());
criteria.put("fileSize",fsFile[r].getSize()>0?fsFile[r].getSize():0.00);
//查询这个文件是否已经下载
int flg = lteDatadistributionMapper.queryFtpTransmissionFile(criteria);
if(flg==0){ //如果没有下载过则下载
File localFile = new File("/data1/ftp_data/"+fileName);
OutputStream is = new FileOutputStream(localFile);
ftpClient.retrieveFile(fsFile[r].getName(), is);
is.close();
String csvName=fileName.replace(".gz","");
//下载完解压成.csv文件
excuteLiuxOrde3r("gzip -d /data1/ftp_data/"+fileName);
//删除第一行表头
String[] cmds = {"/bin/sh","-c","sed -i '1d' /data1/ftp_data/" +csvName};
excuteLiuxOrde3r(cmds);
//替换文件中的!为-
String[] cmds1 = {"/bin/sh","-c","sed -i 's/!/-/g' /data1/ftp_data/" + csvName};
excuteLiuxOrde3r(cmds1);
//替换|^% 为 |
String[] cmds2 = {"/bin/sh","-c","sed -i 's/|^%/|/g' /data1/ftp_data/" + csvName};
excuteLiuxOrde3r(cmds2);
//添加年,月,日
String yyyymmdd = fileName.split("_")[3]; //截取字符串: 20160116.csv
String ymd = yyyymmdd.substring(0,8); //年月日:20160116
String yyyy = yyyymmdd.substring(0,4); //年:2016
String mm = yyyymmdd.substring(4,6); //月:01
String dd = yyyymmdd.substring(6,8); //日:16
//2016|01|12|20160112|
String changeStr =yyyy+"|"+mm+"|"+dd+"|"+ymd+"|";
String[] cmds3 = {"/bin/sh","-c","sed -i 's/^/"+changeStr+"&/g' /data1/ftp_data/" + csvName};
excuteLiuxOrde3r(cmds3);
//记录下载情况
lteDatadistributionMapper.saveFtpTransmissionLog(criteria);
}
}
if(j==fsCity.length){
ftpUtil.changeDirectory("/");
ftpUtil.changeDirectory("/FMS_ALARMDATA/");
ftpUtil.changeDirectory("/"+remotePath2+"/");
}
}
if(i==fs2.length){
ftpUtil.changeDirectory("/");
ftpUtil.changeDirectory("/FMS_ALARMDATA/");
ftpUtil.changeDirectory("/"+remotePath1+"/");
}
}
}
}catch (IOException e){
e.printStackTrace();
}finally {
try{
ftpClient.disconnect();
System.out.println("ftp关闭成功");
} catch (IOException e){
System.out.println("ftp关闭失败");
e.printStackTrace();
}
}
//断开与远程服务器的连接
ftpUtil.disconnect();
}


public boolean changeDirectory(String directory) {
try {
int start = directory.startsWith("/") ? 1 : 0;
int end = directory.indexOf("/", start);
while (true) {
String subDirectory = directory.substring(start, end);
if (!ftpClient.changeWorkingDirectory(subDirectory)) {
if (ftpClient.makeDirectory(subDirectory)) {
ftpClient.changeWorkingDirectory(subDirectory);
} else {
log.info("创建目录:"+subDirectory+"失败");
return false;
}
}
start = end + 1;
end = directory.indexOf("/", start);

            // 检查所有目录是否创建完毕
            if (end <= start) {
                break;
            }
        }
    } catch (Exception e) {
        log.error(this, e);
    }
    return true;
}


ftp登录到根目录,让后进入/FMS...../目录,再进入到/FMS..../GZ/目录,然后进入到/FMS..../GZ/20161012/目录,需要下载20161012文件夹里面的几个文件,然后跳出20161012目录进回到GZ目录,然后进入/FMS..../GZ/20161013/.。依。以此类推

,需要把/FMS....下面的全部目录文件都下载到本地,经过测试,下木有问题,现在要问怎么ftpclient怎么退出当前目录,,,,

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问