|
@@ -7,6 +7,9 @@ import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
import java.net.MalformedURLException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
import org.apache.commons.net.ftp.FTPClient;
|
|
|
import org.apache.commons.net.ftp.FTPFile;
|
|
|
import org.apache.commons.net.ftp.FTPReply;
|
|
@@ -43,6 +46,7 @@ public class FtpUtil {
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
+ throw new RuntimeException("登陆ftp服务器失败");
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
@@ -207,4 +211,60 @@ public class FtpUtil {
|
|
|
closeFtpClient(ftpClient);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * * 下载新文件
|
|
|
+ * @param ftpDir FTP服务器文件目录
|
|
|
+ * @param ftpFileNames 文件名称
|
|
|
+ * @param localpath 下载后的文件路径
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<String> localNewFiles(String ftpDir, List<String> ftpFileNames, String localpath) {
|
|
|
+ List<String> localNewFiles = new ArrayList<>();
|
|
|
+ OutputStream os = null;
|
|
|
+ //检查文件是否存在
|
|
|
+ if (ftpFileNames != null && ftpFileNames.size() > 0){
|
|
|
+ FTPClient ftpClient = getFtpClient();
|
|
|
+ try {
|
|
|
+ ftpClient.changeWorkingDirectory(ftpDir);
|
|
|
+ int replyCode = ftpClient.getReplyCode();
|
|
|
+ if (!FTPReply.isPositiveCompletion(replyCode)) {
|
|
|
+ throw new RuntimeException("ftp服务目录不存在");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return localNewFiles;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (String ftpFileName:ftpFileNames) {
|
|
|
+ try {
|
|
|
+ FTPFile[] ftpFileArr = ftpClient.listFiles(ftpFileName);
|
|
|
+ //文件存在下载到本地
|
|
|
+ if (null == ftpFileArr || ftpFileArr.length > 0) {
|
|
|
+ File localFile = new File(localpath + ftpFileName);
|
|
|
+ FileKit.createFile(localpath + ftpFileName);
|
|
|
+ os = new FileOutputStream(localFile);
|
|
|
+ ftpClient.retrieveFile(ftpFileArr[0].getName(), os);
|
|
|
+ os.close();
|
|
|
+ if(!localNewFiles.contains(ftpFileName)){
|
|
|
+ localNewFiles.add(ftpFileName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e2) {
|
|
|
+ e2.printStackTrace();
|
|
|
+ }finally {
|
|
|
+ if (null != os) {
|
|
|
+ try {
|
|
|
+ os.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ closeFtpClient(ftpClient);
|
|
|
+ }
|
|
|
+
|
|
|
+ return localNewFiles;
|
|
|
+ }
|
|
|
}
|