智慧保安后台管理-外网
Administrator
2022-04-27 dd899673f3ff75ad59ae43afd124ef8ef0fc0180
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package org.springblade.modules.FTP;
 
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.springblade.core.tool.api.R;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.io.InputStream;
 
import static org.springblade.common.config.FtpConfig.*;
 
/**
 * FTP读取文件
 * @author
 * @since 2021-04-26 修改
 */
@Component
public class Monitor {
 
    /**
     * 读取文件(对象)
     * @param uuid 随机数
     * @return
     */
    public static Result isFTPFileExist(String uuid) {
        //创建 ftp 对象
        FTPClient ftp = new FTPClient();
        boolean flag = false;
        try {
            //连接
            ftp.connect(ftpHost, ftpPort);
            // 登陆
            ftp.login(ftpUserName, ftpPassword);
            // 检验登陆操作的返回码是否正确
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                ftp.disconnect();
                return new Result(400,null,"ftp 连接失败",null);
            }
 
            ftp.enterLocalActiveMode();
 
            // 设置文件类型为二进制,与ASCII有区别
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
 
            // 设置编码格式
            ftp.setControlEncoding("GBK");
 
            // 检验文件是否存在
            ftp.changeWorkingDirectory(ftpPath);
            FTPFile[] files = ftp.listFiles();
            if (files.length==0){
                return new Result(400,null,"未读取到文件",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);
                        //数据处理
                        Result 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 new Result(200,null,"新增成功",null);
                        }
                        if (result.getCode()==201) {
                            //返回
                            return new Result(201,null,"修改成功",null);
                        }
                        if (result.getCode()==202) {
                            //返回
                            return new Result(202,null,result.getMsg(),null);
                        }
                        if (result.getCode()==203) {
                            //返回
                            return new Result(203,null,result.getMsg(),null);
                        }
                    }
                }
                return new Result(400,null,"未读取到对应的文件",null);
            }
        } catch (Exception e) {
            System.out.println("ftp连接失败");
            e.printStackTrace();
        } finally {
            if (ftp != null) {
                try {
                    ftp.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return new Result(400,null,"失败",null);
    }
 
    /**
     * 读取内外推送的文件,每30秒读取一次
     * @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;
            }
 
            ftp.enterLocalActiveMode();
 
            // 设置文件类型为二进制,与ASCII有区别
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
 
            // 设置编码格式
            ftp.setControlEncoding("GBK");
 
            // 检验文件是否存在
            ftp.changeWorkingDirectory(ftpPath);
            FTPFile[] files = ftp.listFiles();
            if (files.length==0){
                return false;
            }
            else {
                for (FTPFile file : files) {
                    String fileName = file.getName();
                    InputStream is = ftp.retrieveFileStream(new String(fileName.getBytes("GBK"), FTP.DEFAULT_CONTROL_ENCODING));
                    if (null == is) {
                        return false;
                    } else {
                        String substring1 = fileName.substring(0, 4);
                        if (substring1.equals("nsql")) {
                            //把文件下载到本地
                            FtpUtil.downloadFtpFile(ftpHost, ftpUserName, ftpPassword, ftpPort, ftpPath, localPath, fileName);
                            //
                            String s = OutJson.TestJson(fileName);
                            //sql语句
                            String sql = OutJson.stringReplace(s);
                            String[] split = sql.split(";");//以逗号分割
                            for (String sqls : split) {
                                //判断是否是新增,删除,修改
                                String substring = sqls.substring(0, 2);
                                //新增
                                if (substring.equals("in")) {
                                    //运行sql语句
                                    MysqlCenlint.inster(sqls);
                                }
                                //修改
                                else if (substring.equals("up")) {
                                    MysqlCenlint.update(sqls);
                                }
                                //删除
                                else {
                                    MysqlCenlint.delete(sqls);
                                }
                            }
                            //删除本地服务器文件
                            MysqlCenlint.deletess(fileName);
                            //删除 ftp 服务器文件
                            FtpUtil.deleteFile(ftpHost, ftpPort, ftpUserName, ftpPassword, ftpPath, fileName);
                        }
                        //关闭流
                        is.close();
                        ftp.completePendingCommand();
                    }
                    return true;
                }
            }
        } catch (Exception e) {
            System.out.println("ftp连接失败");
            e.printStackTrace();
        } finally {
            if (ftp != null) {
                try {
                    ftp.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }
}