From 96c1fa69d47d1b69422c94d167d7db48e07c2b14 Mon Sep 17 00:00:00 2001 From: wangxuanran <3055088966@qq.com> Date: Wed, 23 Nov 2022 09:59:43 +0800 Subject: [PATCH] =?UTF-8?q?xuanran.wang=202022-11-21=20=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E4=BA=A4=E9=93=B6=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../waco_first/annocation/BodyPath.java | 17 ++ .../traffic_bank/waco_first/entity/Info.java | 33 +++ .../{ => waco_first}/job/WacoDataPushOA.java | 5 +- .../service/WacoDataPushOAService.java | 86 +++++++ .../weaver/xuanran/wang/util/FtpUtil.java | 221 ++++++++++++++++++ .../wang/traffic_bank/TrafficBankTest.java | 10 - .../waco_first/WacoFirstTest.java | 46 ++++ 7 files changed, 406 insertions(+), 12 deletions(-) create mode 100644 src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/annocation/BodyPath.java create mode 100644 src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/entity/Info.java rename src/main/java/weaver/xuanran/wang/traffic_bank/{ => waco_first}/job/WacoDataPushOA.java (74%) create mode 100644 src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/service/WacoDataPushOAService.java create mode 100644 src/main/java/weaver/xuanran/wang/util/FtpUtil.java delete mode 100644 src/test/java/xuanran/wang/traffic_bank/TrafficBankTest.java create mode 100644 src/test/java/xuanran/wang/traffic_bank/waco_first/WacoFirstTest.java diff --git a/src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/annocation/BodyPath.java b/src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/annocation/BodyPath.java new file mode 100644 index 0000000..1810192 --- /dev/null +++ b/src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/annocation/BodyPath.java @@ -0,0 +1,17 @@ +package weaver.xuanran.wang.traffic_bank.waco_first.annocation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + *

+ * + * @Author xuanran.wang + * @Date 2022/11/22 21:15 + */ +@Target({ElementType.FIELD,ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface BodyPath { +} diff --git a/src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/entity/Info.java b/src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/entity/Info.java new file mode 100644 index 0000000..1bbe4d1 --- /dev/null +++ b/src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/entity/Info.java @@ -0,0 +1,33 @@ +package weaver.xuanran.wang.traffic_bank.waco_first.entity; + +import lombok.Data; +import weaver.xuanran.wang.traffic_bank.waco_first.annocation.BodyPath; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +/** + *

威科先行信息实体类

+ * + * @Author xuanran.wang + * @Date 2022/11/22 20:09 + */ +@Data +public class Info { + private String adminPenaltyTitle; + private String trialCourt; + private String jurisdictionName; + private String adminPenaltyNumber; + private String industryClassification; + private String adminPenaltyDate; + private String punishmentObjectType; + private String punishedParties; + private String punishedPeople; + private String lawAccording; + private String punishmentCause; + private String punishmentResult; + private String punishmentAmount; + @BodyPath + private String bodyPath; +} diff --git a/src/main/java/weaver/xuanran/wang/traffic_bank/job/WacoDataPushOA.java b/src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/job/WacoDataPushOA.java similarity index 74% rename from src/main/java/weaver/xuanran/wang/traffic_bank/job/WacoDataPushOA.java rename to src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/job/WacoDataPushOA.java index 323b582..5e10cc6 100644 --- a/src/main/java/weaver/xuanran/wang/traffic_bank/job/WacoDataPushOA.java +++ b/src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/job/WacoDataPushOA.java @@ -1,4 +1,4 @@ -package weaver.xuanran.wang.traffic_bank.job; +package weaver.xuanran.wang.traffic_bank.waco_first.job; import aiyh.utils.Util; import org.apache.log4j.Logger; @@ -6,6 +6,7 @@ import weaver.interfaces.schedule.BaseCronJob; /** *

交银理财威科先行数据写入建模

+ *

每天从sftp服务器上读数据然后写入到建模中

* @Author xuanran.wang * @Date 2022/11/22 16:18 */ @@ -15,6 +16,6 @@ public class WacoDataPushOA extends BaseCronJob { @Override public void execute() { - } + } diff --git a/src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/service/WacoDataPushOAService.java b/src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/service/WacoDataPushOAService.java new file mode 100644 index 0000000..c96cf9a --- /dev/null +++ b/src/main/java/weaver/xuanran/wang/traffic_bank/waco_first/service/WacoDataPushOAService.java @@ -0,0 +1,86 @@ +package weaver.xuanran.wang.traffic_bank.waco_first.service; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import weaver.xuanran.wang.traffic_bank.waco_first.annocation.BodyPath; +import weaver.xuanran.wang.traffic_bank.waco_first.entity.Info; + +import java.io.File; +import java.lang.reflect.Field; +import java.util.Iterator; +import java.util.List; +import java.util.regex.Matcher; + +/** + *

威科先行数据写入建模业务类

+ * + * @Author xuanran.wang + * @Date 2022/11/22 20:03 + */ +public class WacoDataPushOAService { + + /** + *

递归获取指定文件夹下的所有xml信息

+ * @author xuanran.wang + * @dateTime 2022/11/22 21:14 + * @param res 结果集 + * @param path 文件路径 + * @param suffix 后缀 + **/ + public void getAllInfoByPath(List res, String path, String suffix) throws Exception { + File rootFile = new File(path); + if(rootFile.exists()){ + File[] files = rootFile.listFiles(); + if(files != null && files.length > 0){ + for (File childFile : files) { + if(childFile.isDirectory()){ + getAllInfoByPath(res, childFile.getPath(), suffix); + }else { + if (childFile.getName().endsWith(suffix)) { + Info info = xmlStrToObject(path + File.separator + childFile.getName()); + res.add(info); + } + } + } + } + } + } + + /** + *

通过反射将xml转成实体类 DOM4J

+ * @author xuanran.wang + * @dateTime 2022/11/22 20:39 + * @param filePath xml文件地址 + * @return 对象集合 + **/ + public Info xmlStrToObject(String filePath) throws Exception { + //1.创建Reader对象 + SAXReader reader = new SAXReader(); + //2.加载xml + Document document = reader.read(new File(filePath)); + //3.获取根节点 + Element rootElement = document.getRootElement(); + Iterator iterator = rootElement.elementIterator(); + Info info = Info.class.newInstance(); + while (iterator.hasNext()){ + Element stu = (Element) iterator.next(); + Iterator iterator1 = stu.elementIterator(); + while (iterator1.hasNext()){ + Element stuChild = (Element) iterator1.next(); + Field declaredField = info.getClass().getDeclaredField(stuChild.getName()); + declaredField.setAccessible(true); + String value = stuChild.getStringValue(); + BodyPath annotation = declaredField.getDeclaredAnnotation(BodyPath.class); + if(annotation != null){ + String parent = new File(filePath).getParent(); + value = value.replaceAll("./", Matcher.quoteReplacement(File.separator)) + .replaceAll("/", Matcher.quoteReplacement(File.separator)); + value = parent + value; + } + declaredField.set(info, value); + } + } + return info; + } +} diff --git a/src/main/java/weaver/xuanran/wang/util/FtpUtil.java b/src/main/java/weaver/xuanran/wang/util/FtpUtil.java new file mode 100644 index 0000000..c454935 --- /dev/null +++ b/src/main/java/weaver/xuanran/wang/util/FtpUtil.java @@ -0,0 +1,221 @@ +package weaver.xuanran.wang.util; + +import aiyh.utils.Util; +import com.jcraft.jsch.*; +import org.apache.log4j.Logger; + +import java.io.File; +import java.io.FileOutputStream; +import java.nio.file.Files; +import java.util.Iterator; +import java.util.Properties; +import java.util.Vector; + +/** + *

+ * + * @Author xuanran.wang + * @Date 2022/11/22 17:25 + */ +public class FtpUtil { + private final Logger logger = Util.getLogger(); + /** + *

连接ftp

+ * @author xuanran.wang + * @dateTime 2022/11/22 17:29 + * @param host 主机地址 + * @param port 端口号 + * @param username 用户名 + * @param password 密码 + * @return 连接对象 + **/ + public ChannelSftp connect(String host, int port, String username, String password) { + ChannelSftp sftp = null; + Channel channel=null; + Session sshSession=null; + try { + JSch jsch = new JSch(); + jsch.getSession(username, host, port); + sshSession = jsch.getSession(username, host, port); + sshSession.setPassword(password); + Properties sshConfig = new Properties(); + sshConfig.put("StrictHostKeyChecking", "no"); + sshSession.setConfig(sshConfig); + sshSession.connect(); + if(logger.isInfoEnabled()){ + logger.info("***************** Session connected. **********************"); + logger.info("***************** Opening Channel. **********************"); + } + channel = sshSession.openChannel("sftp"); + channel.connect(); + sftp = (ChannelSftp) channel; + if(logger.isInfoEnabled()){ + logger.info("***************** Connected to " + host + ". **********************"); + } + } catch (Throwable e) { + if (channel!=null) { + try { + channel.disconnect(); + }catch(Throwable e1) { + } + } + + if (sshSession!=null) { + try { + sshSession.disconnect(); + }catch(Throwable e1) { + } + } + + logger.error(e.getMessage(), e); + } + return sftp; + } + + /** + * 关闭连接 + * @param sftp + */ + public void disconnect(String host, ChannelSftp sftp){ + // 关闭连接 + try { + if (null != sftp) { + sftp.disconnect(); + if(logger.isInfoEnabled()){ + logger.info("***************** Closing Channel. **********************"); + } + if (null != sftp.getSession()) { + sftp.getSession().disconnect(); + if(logger.isInfoEnabled()){ + logger.info("***************** Session disconnect. **********************"); + } + } + } + if (logger.isInfoEnabled()) { + logger.info("**************** Disconnect to " + host + ". *******************"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 判断目录下是否存在文件或者文件夹 + * @param directory 目录 + * @param fileName 文件 + * @param sftp 连接 + * @return 是否存在 + */ + public boolean isExist(String directory, String fileName, ChannelSftp sftp) { + try { + Vector v = listFiles(directory, sftp); + Iterator it = v.iterator(); + while (it.hasNext()) { + ChannelSftp.LsEntry entry = it.next(); + String flName = entry.getFilename(); + if (flName.equals(fileName)) { + return true; + } + } + } catch (SftpException e) { + e.printStackTrace(); + } + return false; + } + + /** + * 上传文件 + * @param directory 上传的目录 + * @param uploadFile 要上传的文件 + * @param sftp 连接 + */ + public boolean upload(String directory, String uploadFile, ChannelSftp sftp) { + boolean successFlg = true; + try { + sftp.cd(directory); + File file = new File(uploadFile); + sftp.put(Files.newInputStream(file.toPath()), file.getName()); + if(logger.isInfoEnabled()){ + logger.info("***************** Finished **********************"); + } + } catch (Exception e) { + successFlg = false; + } + return successFlg; + } + + /** + * 下载文件 + * @param directory 下载目录 + * @param downloadFile 下载的文件 + * @param saveFile 存在本地的路径 + * @param sftp 连接 + */ + public boolean download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) { + boolean successFlg = true; + try { + logger.info("下载目录为 " + directory); + sftp.cd(directory); + File file = new File(saveFile); + if(!file.getParentFile().exists()){ + file.getParentFile().mkdirs(); + } + String pwd = sftp.pwd(); + logger.info("pwd => " + pwd); + logger.info("downloadFile => " + downloadFile); + sftp.get(downloadFile, new FileOutputStream(file)); + if(logger.isInfoEnabled()){ + logger.info("***************** Finished **********************"); + } + } catch (Exception e) { + successFlg = false; + logger.info("下载文件出现异常 => " + e.getMessage()); + } + return successFlg; + } + + /** + * 删除文件 + * @param directory 要删除文件所在目录 + * @param deleteFile 要删除的文件 + * @param sftp 连接 + */ + public void delete(String directory, String deleteFile, ChannelSftp sftp) { + try { + sftp.cd(directory); + sftp.rm(deleteFile); + if(logger.isInfoEnabled()){ + logger.info("***************** Finished **********************"); + } + if (null != sftp) { + sftp.disconnect(); + if (null != sftp.getSession()) { + sftp.getSession().disconnect(); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 列出目录下的文件 + * @param directory 要列出的目录 + * @param sftp 连接 + * @return + * @throws SftpException + */ + public Vector listFiles(String directory, ChannelSftp sftp) throws SftpException { + return sftp.ls(directory); + } + + /** + * 判断本地路径是否存在,不存在就创建路径 + */ + public void makeDirs(String localSavePath) { + File localFile = new File(localSavePath); + if (!localFile.exists()) { + localFile.mkdirs(); + } + } +} diff --git a/src/test/java/xuanran/wang/traffic_bank/TrafficBankTest.java b/src/test/java/xuanran/wang/traffic_bank/TrafficBankTest.java deleted file mode 100644 index 725e00a..0000000 --- a/src/test/java/xuanran/wang/traffic_bank/TrafficBankTest.java +++ /dev/null @@ -1,10 +0,0 @@ -package xuanran.wang.traffic_bank; - -/** - *

- * - * @Author xuanran.wang - * @Date 2022/11/22 16:50 - */ -public class TrafficBankTest { -} diff --git a/src/test/java/xuanran/wang/traffic_bank/waco_first/WacoFirstTest.java b/src/test/java/xuanran/wang/traffic_bank/waco_first/WacoFirstTest.java new file mode 100644 index 0000000..d8808d5 --- /dev/null +++ b/src/test/java/xuanran/wang/traffic_bank/waco_first/WacoFirstTest.java @@ -0,0 +1,46 @@ +package xuanran.wang.traffic_bank.waco_first; + +import baseTest.BaseTest; +import com.alibaba.fastjson.JSONObject; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Test; +import weaver.xuanran.wang.traffic_bank.waco_first.entity.Info; +import weaver.xuanran.wang.traffic_bank.waco_first.service.WacoDataPushOAService; + +import java.io.File; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + *

威科先行测试类

+ * + * @Author xuanran.wang + * @Date 2022/11/22 16:50 + */ +public class WacoFirstTest extends BaseTest { + + private WacoDataPushOAService wacoDataPushOAService = new WacoDataPushOAService(); + /** + *

测试解析xml

+ * @author xuanran.wang + * @dateTime 2022/11/22 21:22 + **/ + @Test + public void testParseXml() throws Exception { + ArrayList infos = new ArrayList<>(); + wacoDataPushOAService.getAllInfoByPath(infos, "F:\\wxr\\项目\\交银理财\\威科先行\\20220924","xml"); + System.out.println(infos); + for (Info info : infos) { + System.out.println("解析的对象 \n : "); + System.out.println(JSONObject.toJSONString(info)); + System.out.println("\n"); + System.out.println("文件地址 : " + info.getBodyPath()); + } +// String name = "./2E058366389/body.txt"; +// System.out.println(name.replaceAll("/","a")); + } +}