From dd899673f3ff75ad59ae43afd124ef8ef0fc0180 Mon Sep 17 00:00:00 2001
From: Administrator <admin>
Date: Wed, 27 Apr 2022 17:24:12 +0800
Subject: [PATCH] ftp 同步修改调整
---
src/main/java/org/springblade/modules/FTP/Monitor.java | 150 ++++++++++++++++++-------------------------------
1 files changed, 56 insertions(+), 94 deletions(-)
diff --git a/src/main/java/org/springblade/modules/FTP/Monitor.java b/src/main/java/org/springblade/modules/FTP/Monitor.java
index b883ab7..52c6e41 100644
--- a/src/main/java/org/springblade/modules/FTP/Monitor.java
+++ b/src/main/java/org/springblade/modules/FTP/Monitor.java
@@ -21,14 +21,12 @@
@Component
public class Monitor {
-
-
/**
* 读取文件(对象)
* @param uuid 随机数
* @return
*/
- public static R isFTPFileExist(String uuid) {
+ public static Result isFTPFileExist(String uuid) {
//创建 ftp 对象
FTPClient ftp = new FTPClient();
boolean flag = false;
@@ -40,7 +38,7 @@
// 检验登陆操作的返回码是否正确
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
ftp.disconnect();
- return R.data(400,null,"ftp 连接失败");
+ return new Result(400,null,"ftp 连接失败",null);
}
ftp.enterLocalActiveMode();
@@ -55,7 +53,7 @@
ftp.changeWorkingDirectory(ftpPath);
FTPFile[] files = ftp.listFiles();
if (files.length==0){
- return R.data(400,null,"未读取到文件");
+ return new Result(400,null,"未读取到文件",null);
} else {
for (FTPFile file : files){
String fileName = file.getName();
@@ -67,7 +65,7 @@
// 解析数据
String s = OutJson.TestJson(fileName);
//数据处理
- R result = DataHandler.handler(s,uuid);
+ Result result = DataHandler.handler(s,uuid);
//删除本地文件
MysqlCenlint.deletess(fileName);
FtpUtil.deleteFile(ftpHost, ftpPort, ftpUserName, ftpPassword, ftpPath, fileName);
@@ -75,11 +73,23 @@
ftp.completePendingCommand();
if (result.getCode()==200) {
//返回
- return R.data(result);
+ 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 R.data(400,null,"未读取到对应的文件");
+ return new Result(400,null,"未读取到对应的文件",null);
}
} catch (Exception e) {
System.out.println("ftp连接失败");
@@ -93,14 +103,14 @@
}
}
}
- return R.data(400,null,"失败");
+ return new Result(400,null,"失败",null);
}
/**
- * 读取文件
+ * 读取内外推送的文件,每30秒读取一次
* @return
*/
- // @Scheduled(cron = "*/30 * * * * ?")
+ @Scheduled(cron = "*/30 * * * * ?")
public static boolean isFTPFileExist() {
FTPClient ftp = new FTPClient();
try {
@@ -129,110 +139,62 @@
return false;
}
else {
- for (FTPFile file : files){
+ 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);
- //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);
+ 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);
+ }
}
- //修改
- else if (substring.equals("up")) {
- MysqlCenlint.update(sqls);
- }
- //删除
- else {
- MysqlCenlint.delete(sqls);
- }
+ //删除本地服务器文件
+ MysqlCenlint.deletess(fileName);
+ //删除 ftp 服务器文件
+ FtpUtil.deleteFile(ftpHost, ftpPort, ftpUserName, ftpPassword, ftpPath, fileName);
}
- //删除本地文件
- //MysqlCenlint.deletes(fileName);
- MysqlCenlint.deletess(fileName);
- FtpUtil.deleteFile(ftpHost, ftpPort, ftpUserName, ftpPassword, ftpPath, fileName);
+ //关闭流
is.close();
ftp.completePendingCommand();
}
+ return true;
}
- return true;
}
-
- //InputStream is = ftp.retrieveFileStream(new String(file.getBytes("GBK"), FTP.DEFAULT_CONTROL_ENCODING));
-
-// if (is == null || ftp.getReplyCode() == FTPReply.FILE_UNAVAILABLE) {
-// return false;
-//
-// }
-//
-// if (is != null) {
-// //把文件下载到本地
-// FtpUtil.downloadFtpFile(ftpHost, ftpUserName, ftpPassword, ftpPort, ftpPath, localPath, fileName);
-// //
-// String s = OutJson.TestJson();
-// //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.delete();
-// FtpUtil.deleteFile(ftpHost, ftpPort, ftpUserName, ftpPassword, "anbao/", "nsql.json");
-// is.close();
-// ftp.completePendingCommand();
-//
-// }
-
-
} catch (Exception e) {
System.out.println("ftp连接失败");
e.printStackTrace();
-
} finally {
-
if (ftp != null) {
-
try {
-
ftp.disconnect();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
return false;
-
}
}
--
Gitblit v1.9.3