/**
* 递归下载文件
*
* @param localPath
* @param remotePath
*/
public void processdownload(String localPath, String remotePath) {
FileOutputStream os = null;
try {
ArrayList listfileName = getNameList(remotePath);
ftpClient.cd(remotePath);
ftpClient.binary();
for (int j = 0; j < listfileName.size(); j++) {
TelnetInputStream is = ftpClient.get(listfileName.get(j).toString());
File file_out = new File(localPath + File.separator + listfileName.get(j).toString());
os = new FileOutputStream(file_out);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
deleteLoadFile(listfileName.get(j).toString());
System.out.println("成功下载文件:" + remotePath + File.separator + listfileName.get(j).toString());
}
ftpClient.closeServer();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
os.close();
ftpClient.closeServer();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
删除ftp上的文件
public void deleteLoadFile(String fileName) throws Exception {
try {
if (ftpClient != null) {
String del = "DELE " + fileName + " \r\n";
System.out.print(del); // 输出执行的命令
ftpClient.sendServer(del);
ftpClient.readServerResponse();
}
} catch (IOException e) {
throw new Exception("ftp delete file error:" + e.getMessage());
}
}