FTP获取最新文件!

来源:互联网 发布:绘制平面图软件 编辑:程序博客网 时间:2024/06/05 11:02
for (FTPFile file : ftpUtils1.getClient().directoryList()) {    if (!file.isFile()) {        continue;    }    Date dateTemp = file.lastModified();    if (date == null) {        date = dateTemp;        remoteFileName = file.getName();    } else {        if (date.compareTo(dateTemp) < 0) {            date = dateTemp;            remoteFileName = file.getName();        }    }}
ftputils
package ftpTrance;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;import java.util.Calendar;import java.util.Date;import org.apache.commons.net.ftp.FTP;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;import org.apache.commons.net.ftp.FTPReply;import org.apache.log4j.Logger;import util.FTPUtil;import util.FileCopyUtil;import baseClass.ApplicationConfiguration;import baseClass.EnergydbServiceTemplate;import fdgh.energydb.DataValue;public class FTPUtils extends EnergydbServiceTemplate {   private static final Logger log = Logger.getLogger(FileCopyUtil.class);   String downloadUrl = ApplicationConfiguration.getConfiguration()         .getProperty("downloadUrl");   String downloadUser = ApplicationConfiguration.getConfiguration()         .getProperty("downloadUser");   String downloadPassword = ApplicationConfiguration.getConfiguration()         .getProperty("downloadPassword");   String downloadPort = ApplicationConfiguration.getConfiguration()         .getProperty("downloadPort");   String uploadUrl = ApplicationConfiguration.getConfiguration().getProperty(         "uploadUrl");   String uploadUser = ApplicationConfiguration.getConfiguration()         .getProperty("uploadUser");   String uploadPassword = ApplicationConfiguration.getConfiguration()         .getProperty("uploadPassword");   String uploadPort = ApplicationConfiguration.getConfiguration()         .getProperty("uploadPort");   // 枚举类UploadStatus代码   public enum UploadStatus {      Create_Directory_Fail, // 远程服务器相应目录创建失?      Create_Directory_Success, // 远程服务器闯将目录成?      Upload_New_File_Success, // 上传新文件成?      Upload_New_File_Failed, // 上传新文件失?      File_Exits, // 文件已经存在      Remote_Bigger_Local, // 远程文件大于本地文件      Upload_From_Break_Success, // 断点续传成功      Upload_From_Break_Failed, // 断点续传失败      Delete_Remote_Faild; // 删除远程文件失败   }   // 枚举类DownloadStatus代码   public enum DownloadStatus {      Remote_File_Noexist, // 远程文件不存在      Local_Bigger_Remote, // 本地文件大于远程文件      Download_From_Break_Success, // 断点下载文件成功      Download_From_Break_Failed, // 断点下载文件失败      Download_New_Success, // 全新下载文件成功      Download_New_Failed; // 全新下载文件失败   }   public FTPClient ftpClient = new FTPClient();   private String ftpURL, username, pwd, ftpport, file1, file2;   public FTPUtils(String _ftpURL, String _username, String _pwd,         String _ftpport, String _file1, String _file2) {      // 设置将过程中使用到的命令输出到控制台      ftpURL = _ftpURL;      username = _username;      pwd = _pwd;      ftpport = _ftpport;      file1 = _file1;      file2 = _file2;      this.ftpClient.addProtocolCommandListener(new PrintCommandListener(            new PrintWriter(System.out)));   }   public FTPUtils(String _ftpURL, String _username, String _pwd,         String _ftpport) {      // 设置将过程中使用到的命令输出到控制台      ftpURL = _ftpURL;      username = _username;      pwd = _pwd;      ftpport = _ftpport;      this.ftpClient.addProtocolCommandListener(new PrintCommandListener(            new PrintWriter(System.out)));   }   /** */   /**    * 连接到FTP服务?    *     * @param hostname    *            主机?    * @param port    *            端口    * @param username    *            用户?    * @param password    *            密码    * @return 是否连接成功    * @throws IOException    */   public boolean connect(String hostname, int port, String username,         String password) throws Exception {      ftpClient.connect(hostname, port);      ftpClient.setControlEncoding("GBK");      if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {         if (ftpClient.login(username, password)) {            return true;         }      }      disconnect();      return false;   }   /** */   /**    * 断开与远程服务器的连接    *     * @throws IOException    */   public void disconnect() throws Exception {      if (ftpClient.isConnected()) {         ftpClient.disconnect();      }   }   public void downLoadNewUrl(FTPUtils downloadFtp, String remote, String local)         throws Exception {      downloadFtp.download(remote, local);   }   public void downLoadNewUrl(String hostname, int port, String username,         String password, String remote, String local) throws Exception {      FTPUtils ftpUtils = new FTPUtils(hostname, username, password, port            + "");      ftpUtils.connect(hostname, 21, username, password);      download(remote, local);      ftpUtils.disconnect();   }   /** */   /**    * 从FTP服务器上下载文件,支持断点续传,上传百分比汇报    *     * @param remote    *            远程文件路径    * @param local    *            本地文件路径    * @return 上传的状?    * @throws IOException    */   public DownloadStatus download(String remote, String local)         throws IOException {      log.info("------> local file path is :" + local            + "  remote file path is :" + remote);      /*       * // 设置被动模式 ftpClient.enterLocalPassiveMode(); // 设置以二进制方式传输       * ftpClient.setFileType(FTP.BINARY_FILE_TYPE);       */      DownloadStatus result;      // 检查远程文件是否存在      FTPFile[] files = ftpClient.listFiles(new String(            remote.getBytes("GBK"), "UTF-8"));      if (files.length != 1) {         log.info("远程文件" + remote + "不存在");         return DownloadStatus.Remote_File_Noexist;      }      long lRemoteSize = files[0].getSize();      File f = new File(local);      // 本地存在文件,进行断点下载      if (f.exists()) {         long localSize = f.length();         // 判断本地文件大小是否大于远程文件大小         if (localSize >= lRemoteSize) {            log.info("本地文件大于远程文件,下载中止");            return DownloadStatus.Local_Bigger_Remote;         }         // 进行断点续传,并记录状态         FileOutputStream out = new FileOutputStream(f, true);         ftpClient.setRestartOffset(localSize);         InputStream in = ftpClient.retrieveFileStream(new String(remote               .getBytes("GBK"), "UTF-8"));         byte[] bytes = new byte[1024];         long step = lRemoteSize / 100;         long process = localSize / step;         int c;         while ((c = in.read(bytes)) != -1) {            out.write(bytes, 0, c);            localSize += c;            long nowProcess = localSize / step;            if (nowProcess > process) {               process = nowProcess;               if (process % 10 == 0)                  log.info("下载进度" + process);            }         }         in.close();         out.close();         boolean isDo = ftpClient.completePendingCommand();         if (isDo) {            result = DownloadStatus.Download_From_Break_Success;         } else {            result = DownloadStatus.Download_From_Break_Failed;         }      } else {         OutputStream out = new FileOutputStream(f);         InputStream in = ftpClient.retrieveFileStream(new String(remote               .getBytes("GBK"), "UTF-8"));         byte[] bytes = new byte[1024];         long step = lRemoteSize / 100;         long process = 0;         long localSize = 0L;         int c;         while ((c = in.read(bytes)) != -1) {            out.write(bytes, 0, c);            localSize += c;            long nowProcess = localSize / step;            if (nowProcess > process) {               process = nowProcess;               if (process % 10 == 0)                  log.info("下载进度" + process);            }         }         in.close();         out.close();         boolean upNewStatus = ftpClient.completePendingCommand();         if (upNewStatus) {            result = DownloadStatus.Download_New_Success;         } else {            result = DownloadStatus.Download_New_Failed;         }      }      return result;   }   public void uploadNewUrl(FTPUtils uploadFtp, String local, String remote)         throws IOException {      uploadFtp.upload(local, remote);   }   public void uploadNewUrl(String hostname, int port, String username,         String password, String local, String remote) throws Exception {      FTPUtils ftpUtils = new FTPUtils(hostname, username, password, port            + "");      ftpUtils.connect(hostname, 21, username, password);      log.info("上传文件ftp url :" + hostname + "用户名:" + username + " 密码:"            + password + "上传远程ftp路径:" + remote);      upload(local, remote);      log.info("上传文件成功");      ftpUtils.disconnect();   }   /**    * 上传文件到FTP服务器,支持断点续传    *     * @param local    *            本地文件名称,绝对路径    * @param remote    *            远程文件路径,使用/home/directory1/subdirectory/file.ext    *            按照Linux上的路径指定方式,支持多级目录嵌套,支持递归创建不存在的目录结构    * @return 上传结果    * @throws IOException    */   public UploadStatus upload(String local, String remote) throws IOException {      log.info("------> local file path is :" + local            + "  remote file path is :" + remote);      /*       * // 设置PassiveMode传输 ftpClient.enterLocalPassiveMode(); // 设置以二进制流的方式传输       * ftpClient.setFileType(FTP.BINARY_FILE_TYPE);       */      UploadStatus result;      // 对远程目录的处理      String remoteFileName = remote;      if (remote.contains("/")) {         remoteFileName = remote.substring(remote.lastIndexOf("/") + 1);         String directory = remote.substring(0, remote.lastIndexOf("/") + 1);         if (!directory.equalsIgnoreCase("/")               && !ftpClient.changeWorkingDirectory(directory)) {            // 如果远程目录不存在,则递归创建远程服务器目录            int start = 0;            int end = 0;            if (directory.startsWith("/")) {               start = 1;            } else {               start = 0;            }            end = directory.indexOf("/", start);            while (true) {               String subDirectory = remote.substring(start, end);               if (!ftpClient.changeWorkingDirectory(subDirectory)) {                  if (ftpClient.makeDirectory(subDirectory)) {                     ftpClient.changeWorkingDirectory(subDirectory);                  } else {                     log.info("创建目录失败");                     return UploadStatus.Create_Directory_Fail;                  }               }               start = end + 1;               end = directory.indexOf("/", start);               // 检查所有目录是否创建完毕               if (end <= start) {                  break;               }            }         }      }      log.info("远程目录处理动作结束");      // 检查远程是否存在文件      FTPFile[] files = ftpClient.listFiles(remoteFileName);      if (files.length == 1) {         long remoteSize = files[0].getSize();         File f = new File(local);         long localSize = f.length();         if (remoteSize == localSize) {            return UploadStatus.File_Exits;         } else if (remoteSize > localSize) {            return UploadStatus.Remote_Bigger_Local;         }         // 尝试移动文件内读取指针,实现断点续传         InputStream is = new FileInputStream(f);         if (is.skip(remoteSize) == remoteSize) {            ftpClient.setRestartOffset(remoteSize);            if (ftpClient.storeFile(remote, is)) {               return UploadStatus.Upload_From_Break_Success;            }         }         // 如果断点续传没有成功,则删除服务器上文件,重新上传         if (!ftpClient.deleteFile(remoteFileName)) {            return UploadStatus.Delete_Remote_Faild;         }         is = new FileInputStream(f);         if (ftpClient.storeFile(remote, is)) {            result = UploadStatus.Upload_New_File_Success;         } else {            result = UploadStatus.Upload_New_File_Failed;         }         is.close();      } else {         InputStream is = new FileInputStream(local);         if (ftpClient.storeFile(remoteFileName, is)) {            result = UploadStatus.Upload_New_File_Success;         } else {            result = UploadStatus.Upload_New_File_Failed;         }         is.close();      }      return result;   }   /** */   /**    * 递归创建远程服务器目录    *     * @param remote    *            远程服务器文件绝对路径    * @param ftpClient    *            FTPClient 对象    * @return 目录创建是否成功    * @throws IOException    */   public UploadStatus CreateDirecroty(String remote, FTPClient ftpClient)         throws IOException {      UploadStatus status = UploadStatus.Create_Directory_Success;      String directory = remote.substring(0, remote.lastIndexOf("/") + 1);      if (!directory.equalsIgnoreCase("/")            && !ftpClient.changeWorkingDirectory(new String(directory                  .getBytes("GBK"), "UTF-8"))) {         // 如果远程目录不存在,则递归创建远程服务器目录         int start = 0;         int end = 0;         if (directory.startsWith("/")) {            start = 1;         } else {            start = 0;         }         end = directory.indexOf("/", start);         while (true) {            String subDirectory = new String(remote.substring(start, end)                  .getBytes("GBK"), "UTF-8");            if (!ftpClient.changeWorkingDirectory(subDirectory)) {               if (ftpClient.makeDirectory(subDirectory)) {                  ftpClient.changeWorkingDirectory(subDirectory);               } else {                  log.info("创建目录失败");                  return UploadStatus.Create_Directory_Fail;               }            }            start = end + 1;            end = directory.indexOf("/", start);            // ?查所有目录是否创建完?            if (end <= start) {               break;            }         }      }      return status;   }   public String getLastModifyFileNames(String folderName) throws IOException {      String resultFileName = "";      // 设置PassiveMode传输      ftpClient.enterLocalPassiveMode();      // 设置以二进制流的方式传输      ftpClient.setFileType(FTP.BINARY_FILE_TYPE);      ftpClient.setControlEncoding("GBK");      FTPFile[] files = ftpClient.listFiles(new String(folderName            .getBytes("GBK"), "UTF-8"));      if (files.length <= 2) {         return "";      }      Long[] longArr = new Long[files.length - 2];      for (int i = 0; i < files.length; i++) {         FTPFile file = files[i];         if (file.getName().endsWith(".txt")) {            longArr[i - 2] = file.getTimestamp().getTimeInMillis();         }      }      Long[] strArr = getMaxLastTime(longArr);      long value = (strArr != null && strArr.length > 0) ? strArr[strArr.length - 1]            : 0;      for (int i = 0; i < files.length; i++) {         FTPFile file = files[i];         if (file.getName().endsWith(".txt")) {            if (value == file.getTimestamp().getTimeInMillis()) {               resultFileName = file.getName() + ","                     + file.getTimestamp().getTimeInMillis();            }         }      }      return resultFileName;   }   /**    * 扫描该文件夹下的文件,最后更新的文件时间写入FileConfig.properties文件    *     * @param folderName    *            文件夹    * @throws IOException    */   public void wirteFileLastTime(String folderName) throws IOException {      // 设置PassiveMode传输      ftpClient.enterLocalPassiveMode();      // 设置以二进制流的方式传输      ftpClient.setFileType(FTP.BINARY_FILE_TYPE);      ftpClient.setControlEncoding("GBK");      FTPFile[] files = ftpClient.listFiles(new String(folderName            .getBytes("GBK"), "UTF-8"));      // ftpClient.setf      // String tempLastTimeStr = "";      if (files.length < 3) {         ConfigInfo.writeProperties(Const.proFilePath, folderName, "");         return;      }      Long[] longArr = new Long[files.length - 2];      for (int i = 0; i < files.length; i++) {         FTPFile file = files[i];         if (file.getName().endsWith(".txt")) {            longArr[i - 2] = file.getTimestamp().getTimeInMillis();            /*             * if(tempLastTimeStr.equals("")){ tempLastTimeStr =             * file.getTimestamp().getTimeInMillis()+""; }else{             * tempLastTimeStr = tempLastTimeStr             * +","+file.getTimestamp().getTimeInMillis(); }             */         }      }      Long[] strArr = getMaxLastTime(longArr);      String value = (strArr != null && strArr.length > 0) ? strArr[strArr.length - 1]            + ""            : "";      ConfigInfo.writeProperties(Const.proFilePath, folderName, value);   }   // 冒泡比较   public Long[] getMaxLastTime(Long[] longArr) {      for (int i = 0; i < longArr.length; i++) {         for (int j = 0; j < longArr.length - i - 1; j++) {            Long temp = 0l;            if (longArr[j] > longArr[j + 1]) {               temp = longArr[j];               longArr[j] = longArr[j + 1];               longArr[j + 1] = temp;            }         }      }      return longArr;   }   /**    * 检查是否有新文件,如果有则执行下载上传操作    *     * @param folderName    * @param time    * @throws IOException    */   public void doNewFile(String folderName, long lastTime,         FTPUtils downloadFtp, FTPUtils uploadFtp, DataValue dv)         throws Exception {      FTPFile[] files = ftpClient.listFiles(new String(folderName            .getBytes("GBK"), "UTF-8"));      for (int i = 0; i < files.length; i++) {         FTPFile file = files[i];         if (file.getName().endsWith(".txt")) {            if (file.getTimestamp().getTimeInMillis() >= lastTime) {               log.info("开始执行下载动作");               downloadFtp.download(folderName + "//" + file.getName(),                     formatFileName(file.getTimestamp(), folderName,                           Const.formatDownLoadFileNameFlag));               log.info("执行下载动作完成");               if (dv != null                     && dv.getString("modifyFilename").equals(                           file.getName())) {                  continue;               }               log.info("开始执行上传动作");               uploadFtp.upload(formatFileName(file.getTimestamp(),                     folderName, Const.formatDownLoadFileNameFlag),                     formatFileName(file.getTimestamp(), folderName,                           Const.formatUploadFileNameFlag));               log.info("执行上传动作完成");            }         }      }      log.info("扫描文件并执行下载上传动作完毕");   }   public void doNewFTPFiles(String folderName, long lastTime,         FTPUtils downloadFtp, FTPUtil uploadFtp, DataValue dv)         throws Exception {      FTPFile[] files = ftpClient.listFiles(new String(folderName            .getBytes("GBK"), "UTF-8"));      for (int i = 0; i < files.length; i++) {         FTPFile file = files[i];         if (file.getName().endsWith(".txt")) {            if (file.getTimestamp().getTimeInMillis() >= lastTime) {               log.info("开始执行下载动作");               downloadFtp.download(folderName + "//" + file.getName(),                     formatFileName(file.getTimestamp(), folderName,                           Const.formatDownLoadFileNameFlag));               log.info("执行下载动作完成");               if (dv != null                     && dv.getString("modifyFilename").equals(                           file.getName())) {                  continue;               }               log.info("开始执行上传动作");               uploadFtp                     .ftpUpload(formatFileName(file.getTimestamp(),                           folderName,                           Const.formatDownLoadFileNameFlag), "\\");               // uploadFtp.upload(formatFileName(file.getTimestamp(),               // folderName,               // Const.formatDownLoadFileNameFlag),               // formatFileName(file.getTimestamp(), folderName,               // Const.formatUploadFileNameFlag));               log.info("执行上传动作完成");            }         }      }   }   /**    * 检查文件夹下是否有新文件    *     * @param folderName    * @throws IOException    */   public void checkNewFile(String folderName) throws Exception {      // 设置PassiveMode传输      ftpClient.enterLocalPassiveMode();      // 设置以二进制流的方式传输      ftpClient.setFileType(FTP.BINARY_FILE_TYPE);      ftpClient.setControlEncoding("GBK");      FTPFile[] files = ftpClient.listFiles(new String(folderName            .getBytes("GBK"), "UTF-8"));      for (int i = 0; i < files.length; i++) {         FTPFile file = files[i];         if (file.getName().endsWith(".txt")) {            String folderNameValue = ConfigInfo.readValue(                  Const.proFilePath, folderName);            long lastTime = Long.parseLong(folderNameValue);            if (file.getTimestamp().getTimeInMillis() > lastTime) {               download(folderName + "//" + file.getName(),                     formatFileName(file.getTimestamp(), folderName,                           Const.formatDownLoadFileNameFlag));               uploadNewUrl(downloadUrl, 21, downloadUser,                     downloadPassword, formatFileName(file                           .getTimestamp(), folderName,                           Const.formatUploadFileNameFlag),                     formatFileName(file.getTimestamp(), folderName,                           Const.formatUploadFileNameFlag));               /*                * upload(formatFileName(file.getTimestamp(), folderName,                * Const.formarDownLoadFileNameFlag),                * formatFileName(file.getTimestamp(), folderName,                * Const.formarDownLoadFileNameFlag));                */            }         }      }   }   /**    * 重新命名本地文件    *     * @param cal    *            远程文件日期    * @param folderName    *            文件夹名称    * @param flag    *            判断表示,用于生成下载和上传的文件名    * @return    */   public String formatFileName(Calendar cal, String folderName, String flag) {      String result = "";      Calendar calendar = DateUtils.formatDateToCalendar(cal.getTime());      int year = calendar.get(Calendar.YEAR) - 2000;      int month = calendar.get(Calendar.MONTH) + 1;      int day = calendar.get(Calendar.DAY_OF_MONTH);      Date nowDate = new Date();      Calendar nowCalendar = DateUtils.formatDateToCalendar(nowDate);      int nowYear = nowCalendar.get(Calendar.YEAR);      int nowMonth = nowCalendar.get(Calendar.MONTH) + 1;      int nowDay = nowCalendar.get(Calendar.DAY_OF_MONTH);      String todayFolder = DateUtils.formatNum(nowYear) + ""            + DateUtils.formatNum(nowMonth) + ""            + DateUtils.formatNum(nowDay);      checkLocalDiskFolder(Const.diskPath + "\\" + todayFolder);      if (flag.equals(Const.formatUploadFileNameFlag)) {         return Const.inlandAir + folderName + Const.fileSSIM               + DateUtils.formatNum(year) + DateUtils.formatNum(month)               + DateUtils.formatNum(day);      }      // +" ------------>"+month+"---->>"+day);      result = Const.diskPath + "\\" + todayFolder + "\\" + Const.inlandAir            + folderName + Const.fileSSIM + DateUtils.formatNum(year)            + DateUtils.formatNum(month) + DateUtils.formatNum(day);      return result;   }   /**    * 检查本地磁盘上是否存在该文件夹,不存在则新建    *     * @param folderPath    *            文件夹路径    */   public void checkLocalDiskFolder(String folderPath) {      File file = new File(folderPath);      if (!file.exists()) {         file.mkdirs();      }   }   public void uploadSSIMFile() {   }   public void run() {      try {         this.connect(ftpURL, new java.lang.Integer(ftpport), username, pwd);         FTPFile[] ftpFiles = ftpClient.listFiles("\\AZ");         for (int i = 0; i < ftpFiles.length; i++) {         }         // this.download(file1, file2);         this.upload(file2, file1);         this.disconnect();      } catch (Exception e) {      }   }   public static void main(String[] args) {      // FTPUtils ftpUtils = new FTPUtils("202.106.139.26", "mussimin",      // "fb2Sw3yC",      // "21", "test.txt", "E:\\FTPFile\\b.txt");      FTPUtils ftpUtils1 = new FTPUtils("172.20.34.171", "test", "test", "21");      try {         ftpUtils1.connect("172.20.34.171", 21, "test", "test");         ftpUtils1.download("//MUAZSSIM111215", "E:\\123");         ftpUtils1.disconnect();      } catch (Exception e) {         e.printStackTrace();      }      // ftpUtils.run();   }}

0 0
原创粉丝点击