|
|
@@ -231,12 +231,56 @@ public class FtpUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param ftpClient
|
|
|
+ * @return 获取连接到的ftp站点下的文件夹所有文件
|
|
|
+ */
|
|
|
+public static List<File> downloadFileByDirectory(FTPClient ftpClient,String directory,String fileType) {
|
|
|
+ List<File> files = new ArrayList<File>();
|
|
|
+ try {
|
|
|
+ ftpClient.enterLocalPassiveMode();
|
|
|
+ ftpClient.changeWorkingDirectory("/" + directory);// 转移到FTP服务器目录
|
|
|
+ FTPFile[] ftpFiles = ftpClient.listFiles();
|
|
|
+ if (ftpFiles != null && ftpFiles.length > 0) {
|
|
|
+ ArrayList<FTPFile> list = new ArrayList<FTPFile>(Arrays.asList(ftpFiles));
|
|
|
+ Collections.sort(list, new Comparator<FTPFile>() {
|
|
|
+ @Override
|
|
|
+ public int compare(FTPFile o1, FTPFile o2) {
|
|
|
+ long time = o1.getTimestamp().getTime().getTime();
|
|
|
+ long time1 = o2.getTimestamp().getTime().getTime();
|
|
|
+ if (time > time1) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ if (time == time1) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ for (FTPFile file : list) {
|
|
|
+ if (file.getName().toUpperCase().endsWith("." + fileType)) {
|
|
|
+ File localFile = new File(System.getProperty("java.io.tmpdir") + File.separator + file.getName());
|
|
|
+ OutputStream os = new FileOutputStream(localFile);
|
|
|
+ ftpClient.retrieveFile(file.getName(), os);
|
|
|
+ os.close();
|
|
|
+ files.add(localFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ BaseUtil.getLogger().error(e.toString());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return files;
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
*
|
|
|
* @param ftpClient
|
|
|
* @return 获取连接到的ftp站点下的文件夹所有文件
|
|
|
*/
|
|
|
- public static List<File> downloadFileByDirectory(FTPClient ftpClient,String directory,String fileType) {
|
|
|
+ public static List<File> downloadFileByDirectory(FTPClient ftpClient,String directory) {
|
|
|
List<File> files = new ArrayList<File>();
|
|
|
try {
|
|
|
ftpClient.enterLocalPassiveMode();
|
|
|
@@ -259,7 +303,7 @@ public class FtpUtil {
|
|
|
}
|
|
|
});
|
|
|
for (FTPFile file : list) {
|
|
|
- if (file.getName().toUpperCase().endsWith("." + fileType)) {
|
|
|
+ if(!".".equals(file.getName()) && !"..".equals(file.getName())&& !"bak".equals(file.getName())) {
|
|
|
File localFile = new File(System.getProperty("java.io.tmpdir") + File.separator + file.getName());
|
|
|
OutputStream os = new FileOutputStream(localFile);
|
|
|
ftpClient.retrieveFile(file.getName(), os);
|