|
@@ -2,6 +2,7 @@ package com.scbfkj.uni.process;
|
|
|
|
|
|
import com.scbfkj.uni.library.DataFormatUtil;
|
|
|
import com.scbfkj.uni.library.UniReturnUtil;
|
|
|
+import com.scbfkj.uni.system.Config;
|
|
|
import org.apache.commons.net.ftp.FTPClient;
|
|
|
import org.apache.commons.net.ftp.FTPFile;
|
|
|
import org.apache.commons.net.ftp.FTPReply;
|
|
@@ -13,16 +14,14 @@ import java.io.FileOutputStream;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.nio.file.Files;
|
|
|
import java.time.Duration;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.*;
|
|
|
import java.util.zip.GZIPInputStream;
|
|
|
|
|
|
import static org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE;
|
|
|
|
|
|
public class FTP {
|
|
|
private static final int BUFFER_SIZE = 4096;
|
|
|
+ private static final DataBase DATA_BASE = new DataBase();
|
|
|
|
|
|
public Map<String, Object> update(String connectionConfig, String targetPath, String sourcePath) {
|
|
|
return null;
|
|
@@ -37,8 +36,14 @@ public class FTP {
|
|
|
*/
|
|
|
private FTPClient createFtpClient(Map<String, Object> config) throws Exception {
|
|
|
FTPClient ftp = new FTPClient();
|
|
|
- int port = Objects.nonNull(config.get("port")) ? Integer.parseInt(Objects.toString(config.get("port"))) : 21;
|
|
|
- ftp.connect(Objects.toString(config.get("host")), port);
|
|
|
+ int port = 21;
|
|
|
+ String host = Objects.toString(config.get("host"));
|
|
|
+ if(host.contains(":")){
|
|
|
+ String[] split = host.split(":");
|
|
|
+ port = Integer.parseInt(split[1]);
|
|
|
+ host = split[0];
|
|
|
+ }
|
|
|
+ ftp.connect(host, port);
|
|
|
//common-net的ftpclient默认是使用ASCII_FILE_TYPE,文件会经过ASCII编码转换,所以可能会造成文件损坏
|
|
|
ftp.login(Objects.toString(config.get("username")), Objects.toString(config.get("password")));//登录
|
|
|
String timeOutStr = "timeOut";
|
|
@@ -57,9 +62,14 @@ public class FTP {
|
|
|
return ftp;
|
|
|
}
|
|
|
|
|
|
+ public Map<String, Object> downloadByDataSourceId(String datasourceId, String sourcePath) throws Exception {
|
|
|
+ String config = queryConnectionStr(datasourceId);
|
|
|
+ return download(config,sourcePath);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
- * @param sourcePath 组装好的文件名称
|
|
|
* @param connectionConfig 连接fpt的连接信息
|
|
|
+ * @param sourcePath 组装好的文件名称
|
|
|
* @return
|
|
|
*/
|
|
|
public Map<String, Object> download(String connectionConfig, String sourcePath) {
|
|
@@ -134,4 +144,23 @@ public class FTP {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private static String queryConnectionStr(String datasourceId) throws Exception {
|
|
|
+ List<Map<String, Object>> result = DATA_BASE.query(Config.getCenterConnectionStr(), """
|
|
|
+ select host,httpheaders,httpbody
|
|
|
+ from datasource
|
|
|
+ where datasourceid = ? limit 0 offset 1""", datasourceId);
|
|
|
+ if (result.isEmpty()) {
|
|
|
+ throw new RuntimeException("数据源错误:没有找到数据源");
|
|
|
+ }
|
|
|
+ return result.stream().findFirst().map(it -> {
|
|
|
+ HashMap<String, Object> hashMap = new HashMap<>();
|
|
|
+ hashMap.put("url", it.get("host"));
|
|
|
+ hashMap.put("headers", it.get("httpheaders"));
|
|
|
+ hashMap.put("body", it.get("httpbody"));
|
|
|
+ return DataFormatUtil.toString(hashMap);
|
|
|
+ }).get();
|
|
|
+ }
|
|
|
}
|