智慧保安后台管理-外网
Administrator
2022-04-26 6934ffe9ae5aa3e0096a7028d3b70b07e08294aa
src/main/java/org/springblade/modules/FTP/Monitor.java
File was renamed from src/main/java/org/springblade/modules/FTP/monitor.java
@@ -4,6 +4,7 @@
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.springblade.core.tool.api.R;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@@ -12,55 +13,114 @@
import static org.springblade.common.config.FtpConfig.*;
/**
 * FTP读取文件
 * @author
 * @since 2021-04-26 修改
 */
@Component
public class monitor {
public class Monitor {
   @Scheduled(cron = "*/30 * * * * ?")
   public static boolean isFTPFileExist() {
   /**
    * 读取文件(对象)
    * @param uuid 随机数
    * @return
    */
   public static R isFTPFileExist(String uuid) {
      //创建 ftp 对象
      FTPClient ftp = new FTPClient();
      boolean flag = false;
      try {
         // 连接ftp服务器
//         System.out.println("ftpHost = " + ftpHost);
         //连接
         ftp.connect(ftpHost, ftpPort);
         // 登陆
         ftp.login(ftpUserName, ftpPassword);
         // 检验登陆操作的返回码是否正确
         if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            ftp.disconnect();
            return false;
            return R.data(400,null,"ftp 连接失败");
         }
         ftp.enterLocalActiveMode();
         // 设置文件类型为二进制,与ASCII有区别
         ftp.setFileType(FTP.BINARY_FILE_TYPE);
         // 设置编码格式
         ftp.setControlEncoding("GBK");
         // 提取绝对地址的目录以及文件名
         // 检验文件是否存在
         ftp.changeWorkingDirectory(ftpPath);
         FTPFile[] files = ftp.listFiles();
         if (files.length==0){
            return R.data(400,null,"未读取到文件");
         } else {
            for (FTPFile file : files){
               String fileName = file.getName();
               InputStream is = ftp.retrieveFileStream(new String(fileName.getBytes("GBK"), FTP.DEFAULT_CONTROL_ENCODING));
               String substring1 = fileName.substring(0, 1);
               if (substring1.equals("n")){
                  //把文件下载到本地
                  FtpUtil.downloadFtpFile(ftpHost, ftpUserName, ftpPassword, ftpPort, ftpPath, localPath, fileName);
                  // 解析数据
                  String s = OutJson.TestJson(fileName);
                  //数据处理
                  R result = DataHandler.handler(s,uuid);
                  //删除本地文件
                  MysqlCenlint.deletess(fileName);
                  FtpUtil.deleteFile(ftpHost, ftpPort, ftpUserName, ftpPassword, ftpPath, fileName);
                  is.close();
                  ftp.completePendingCommand();
                  if (result.getCode()==200) {
                     //返回
                     return R.data(result);
                  }
               }
            }
            return R.data(400,null,"未读取到对应的文件");
         }
      } catch (Exception e) {
         System.out.println("ftp连接失败");
         e.printStackTrace();
      } finally {
         if (ftp != null) {
            try {
               ftp.disconnect();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
      return R.data(400,null,"失败");
   }
         //ftpPath = ftpPath.replace("ftp://" + ftpHost + ":" + ftpPort + "/", "");
   /**
    * 读取文件
    * @return
    */
   //   @Scheduled(cron = "*/30 * * * * ?")
   public static boolean isFTPFileExist() {
      FTPClient ftp = new FTPClient();
      try {
         //连接
         ftp.connect(ftpHost, ftpPort);
         // 登陆
         ftp.login(ftpUserName, ftpPassword);
         // 检验登陆操作的返回码是否正确
         if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            ftp.disconnect();
            return false;
         }
         //String dir = ftpPath.substring(0, ftpPath.lastIndexOf("/"));
         ftp.enterLocalActiveMode();
         // file = ftpPath.substring(ftpPath.lastIndexOf("/") + 1);
         // 设置文件类型为二进制,与ASCII有区别
         ftp.setFileType(FTP.BINARY_FILE_TYPE);
         // 进入文件所在目录,注意编码格式,以能够正确识别中文目录
         //ftp.changeWorkingDirectory(new String(dir.getBytes("GBK"), FTP.DEFAULT_CONTROL_ENCODING));
         // 设置编码格式
         ftp.setControlEncoding("GBK");
         // 检验文件是否存在
         ftp.changeWorkingDirectory(ftpPath);