zoukankan      html  css  js  c++  java
  • 多线程查询FTP Server上的文件

    情形是这样的,最近做一个自动化的项目,当batch跑成功了,FTP Server上会有特定的生成文件。但是不确定是什么时候会有,大概是batch跑完了5分钟之内吧,所以在脚本里设置检查点的时候,需要每隔一段时间去刷新FTP Server上的文件。

    网上下了个Serv-U去搭建了个本地的FTP Server,很容易就搭建调试好。然后又下了个commons-net-3.3.jar包去处理FTP相关的业务。

    或许可以不需要用多线程,但是试了下,单线程的去退出再重新登录到FTP Server,FTP Server上的文件并不会自动刷新。所以我在main函数里,每隔一段时间就启动一个线程去看看我要找的文件是否存在。

    import java.io.IOException;
    
    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPFile;
    import org.apache.commons.net.ftp.FTPReply;
    
    public class FTPUtil extends Thread {
        private FTPClient ftpClient = new FTPClient();
        private String ftpdict = ".";
        private boolean flag;
        private static boolean find = false;
        private static long timeslot = 10000;
    
        public boolean connect(String host, int port, String username,
                String password) throws IOException {
            ftpClient.connect(host, port);
            if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                if (ftpClient.login(username, password)) {
                    return true;
                }
            }
            ftpClient.disconnect();
            return false;
        }
    
        public boolean fileExist(String dict, String filename) {
            String pattren = "\.{1,}";
            ftpdict = ftpdict + "/" + dict;
            try {
                FTPFile[] files = ftpClient.listFiles(ftpdict);
                for (int i = 0; i < files.length; i++) {
                    if (files[i].getName().equalsIgnoreCase(filename)) {
                        flag = true;
                    }
                    if (files[i].isDirectory()
                            && !files[i].getName().matches(pattren)) {
                        fileExist(files[i].getName(), filename);
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return flag;
        }
    
        public void disconnect() throws IOException {
            if (ftpClient.isConnected()) {
                ftpClient.disconnect();
            }
        }
    
        public void run() {
            System.out.println(getName() + " thread started!");
            FTPUtil ftp = new FTPUtil();
            try {
                ftp.connect("127.0.0.1", 21, "admin", "admin");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            boolean b = ftp.fileExist(ftp.ftpdict, "3.txt");
            System.err.println(b);
            if (b) {
                find = true;
            }
            try {
                ftp.disconnect();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(getName() + " thread ended!");
        }
    
        @SuppressWarnings("static-access")
        public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
            int index = 0;
            while (!find) {
                new FTPUtil().start();
                Thread.currentThread().sleep(timeslot);
                if (index == 4) {
                    return;
                }
                index++;
            }
        }
    }

    我觉得应该有api是去刷新这个FTP Server的,暂时还没有摸索到,有知道的告知一声谢谢咯。

  • 相关阅读:
    Android控件Editext、TextView属性详解
    修改Android签名证书keystore的密码、别名alias以及别名密码
    android 中如何限制 EditText 最大输入字符数
    keytool 错误 java.io.IOException: incorrect AVA format
    Android打包常见错误之Export aborted because fatal lint errors were found
    正则表达式之判断用户注册信息是否为汉字、字母和数字
    Android Dialog 系统样式讲解及透明背景
    Android中自定义Activity和Dialog的位置大小背景和透明度等
    字体在Android View中的输出 drawText
    怎么用CIFilter给图片加上各种各样的滤镜_1
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/4205386.html
Copyright © 2011-2022 走看看