监听FTP文件变化

整个项目的数据入口是监听FTP文件变化实现的。程序通过监听FTP目录内文件的变化(查找那些文件修改日期比上次修改日期晚的,然后在数据库中没有记录的文件),把监听到新增的文件信息保存在数据库表内文件队列表,然后文件校验拓扑从文件队列表获取那些需要处理的文件信息然后进行拓扑处理。

配置文件

ZOOKEEPER_HOST_PORT=10.230.16.71:2181
ZOOKEEPER_HOST=10.230.16.71
ZOOKEEPER_PORT=2181
SERIAL_VERSION=20170922
#redis  IP
REDIS_HOST=10.230.16.77
REDIS_PORT=6379
#程序运行所在的机器IP
LOCAL_IP=10.230.16.77

监听程序入口

package com.lancy.main;

import java.util.List;
import java.util.Properties;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lancy.entity.FTPEntity;
import com.lancy.thread.GlobalFTPListener;
import com.lancy.thread.LocalFTPListener;

/**
 * FTPListenMain FTP监听启动类 ,通过参数控制启动不同的监听
 */
public class FTPListenMain {
   
    private static Logger logger = LoggerFactory.getLogger(FTPListenMain.class);
    private static Properties props = new Properties();
    private static final String profile = "config.properties";
    static {
        try {
            props.load(FTPListenMain.class.getResourceAsStream("/" + profile));
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    //根据zookeeper里初始化的ftp配置信息启动对应的主机FTP的监听程序。zookeeper每配置一个ftp的主机信息,就启动一个监听线程
    public static void main(String[] args) {
        String zk_Host_Port = props.getProperty("ZOOKEEPER_HOST_PORT");
        String local_Ip = props.getProperty("LOCAL_IP");
        String redis_Host = props.getProperty("REDIS_HOST");
        int redis_Port = Integer.parseInt(props.getProperty("REDIS_PORT"));
        try {
            // 参数为为空 不启动
            if (args == null || args.length <= 0) {
                logger.info(">>>>输入参数不正确>>>为空>>>");
                return;
            }
            ZooKeeper zooKeeper = new ZooKeeper(zk_Host_Port, 1000, null);
            if (args.length == 1 && "lnt".equals(args[0])) {
                List<String> listPath = zooKeeper.getChildren("/lnt/ftp", false);
                //获取zookeeper路径/lnt/ftp/下的所有节点,一般是ip地址
                for (String nodePath : listPath) {
                    //nodeData是一个json字符串                    
                    //"{'host':'10.230.16.77','port':21,'username':'lnt','password':'123456','default':0,
'inPath':'input','outPath':'output'}"
                    String nodeData = new String(zooKeeper.getData("/lnt/ftp" + "/" + nodePath, false, null));

                    JSONObject json = JSON.parseObject(nodeData);
                    FTPEntity ftpEntity = new FTPEntity(json);
                    // 开启监听线程
                    if (!ftpEntity.getHost().equals("10.230.16.202")) {
                        logger.info(">>>>>>>>>>>>>>>>>>>开启监听线程,监听:" + ftpEntity.getHost() + ">>>>>>>>>>>>>>>>>>>>");
                        Thread listenLocalFTP = new Thread(new LocalFTPListener(ftpEntity, local_Ip, redis_Host, redis_Port));
                        listenLocalFTP.start();
                    }
                }
            } else if (args.length == 1 && "ykt".equals(args[0])) {
                //差不多代码
            } else if (args.length == 2 && "lnt".equals(args[0]) && "ykt".equals(args[1])) {
                //上面两个代码相加
            } else {
                logger.info(">>>>输入参数不正确>>>>>>");
                return;
            }
            zooKeeper.close();
        } catch (Exception e) {
            logger.info(e.getMessage());
            e.printStackTrace();
        }

    }

}

监听线程类

package com.lancy.thread;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.net.ftp.FTPFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.lancy.common.DButil;
import com.lancy.common.DateUtil;
import com.lancy.common.FTP
  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
监听FTP文件夹的变化,你需要使用FTPClient类来连接到FTP服务器。然后,你可以使用FTPClient类提供的一些方法来监视FTP文件夹的变化,例如: 1. 使用FTPClient.changeWorkingDirectory()方法进入要监视的文件夹。 2. 使用FTPClient.listFiles()方法获取当前文件夹中的所有文件和子文件夹。 3. 使用FTPClient.retrieveFileStream()方法获取指定文件的输入流,并使用BufferedReader读取输入流中的内容。 你可以将以上步骤封装在一个循环中,以便每隔一段时间重复执行。如果发现有文件文件夹被添加到FTP文件夹中,就可以执行相应的操作。 以下是一个示例代码片段,可以用来监听FTP文件夹的变化: ``` FTPClient client = new FTPClient(); client.connect("ftp.example.com"); client.login("username", "password"); while (true) { client.changeWorkingDirectory("/path/to/ftp/folder"); FTPFile[] files = client.listFiles(); for (FTPFile file : files) { if (file.isFile()) { InputStream inputStream = client.retrieveFileStream(file.getName()); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); // 处理文件内容 reader.close(); inputStream.close(); } else if (file.isDirectory()) { // 处理子文件夹 } } Thread.sleep(5000); // 暂停5秒钟 } client.logout(); client.disconnect(); ``` 请注意,上述代码仅提供了基本的框架。你需要根据自己的需求进行修改和完善。此外,还需要考虑到连接失败、文件读取异常等情况的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值