public static File[] copyDir(String ftpIp, int ftpPort, String ftpUser, String ftpPass,
String ftpPath, String tmpPath) throws Exception {
FTPClient ftp = new FTPClient();
try {
ftp.connect(ftpIp, ftpPort);
ftp.login(ftpUser, ftpPass);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
throw new Exception("用户名或密码不正确。");
}
FTPFile[] files = ftp.listFiles(ftpPath);
if (files.length == 0 && ftp.getReplyString().indexOf("fail") > 0) {
throw new Exception("目录不存在。");
}
File tmpDir = new File(tmpPath);
if (!tmpDir.exists()){
tmpDir.mkdirs();
}
for (FTPFile f : files) {
System.out.println(f.getName());
ftp.retrieveFile(ftpPath + "/" + f.getName(),
new FileOutputStream(tmpDir.getAbsolutePath() + File.separator + f.getName()));
}
ftp.logout();
return tmpDir.listFiles();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (Exception e) {
}
}
}
}
转载于:https://my.oschina.net/h2do/blog/514938