修改BaseTest,修复全局变量引用log报空指针异常
commit
618d1b2a3d
|
@ -31,5 +31,7 @@ log
|
|||
/src/test/resources/application.properties
|
||||
/src/test/resources/application.xml
|
||||
|
||||
DirectoryV2.xml
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<trees>
|
||||
<tree path="/src/main/java/weaver/xuanran/wang/traffic_bank" title="交银理财"/>
|
||||
<tree path="/src/main/java/weaver/xuanran/wang/traffic_bank/job" title="计划任务"/>
|
||||
<tree path="/src/main/java/weaver/youhong" title="艾有宏"/>
|
||||
<tree path="/src/main/java/weaver/xuanran" title="王宣然"/>
|
||||
</trees>
|
|
@ -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;
|
||||
|
||||
/**
|
||||
* <h1></h1>
|
||||
*
|
||||
* @Author xuanran.wang
|
||||
* @Date 2022/11/22 21:15
|
||||
*/
|
||||
@Target({ElementType.FIELD,ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface BodyPath {
|
||||
}
|
|
@ -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;
|
||||
|
||||
/**
|
||||
* <h1>威科先行信息实体类</h1>
|
||||
*
|
||||
* @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;
|
||||
}
|
|
@ -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;
|
|||
|
||||
/**
|
||||
* <h1>交银理财威科先行数据写入建模</h1>
|
||||
* <p>每天从sftp服务器上读数据然后写入到建模中</p>
|
||||
* @Author xuanran.wang
|
||||
* @Date 2022/11/22 16:18
|
||||
*/
|
||||
|
@ -15,6 +16,6 @@ public class WacoDataPushOA extends BaseCronJob {
|
|||
|
||||
@Override
|
||||
public void execute() {
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
/**
|
||||
* <h1>威科先行数据写入建模业务类</h1>
|
||||
*
|
||||
* @Author xuanran.wang
|
||||
* @Date 2022/11/22 20:03
|
||||
*/
|
||||
public class WacoDataPushOAService {
|
||||
|
||||
/**
|
||||
* <h1>递归获取指定文件夹下的所有xml信息</h1>
|
||||
* @author xuanran.wang
|
||||
* @dateTime 2022/11/22 21:14
|
||||
* @param res 结果集
|
||||
* @param path 文件路径
|
||||
* @param suffix 后缀
|
||||
**/
|
||||
public void getAllInfoByPath(List<Info> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <h1>通过反射将xml转成实体类 DOM4J</h1>
|
||||
* @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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
/**
|
||||
* <h1></h1>
|
||||
*
|
||||
* @Author xuanran.wang
|
||||
* @Date 2022/11/22 17:25
|
||||
*/
|
||||
public class FtpUtil {
|
||||
private final Logger logger = Util.getLogger();
|
||||
/**
|
||||
* <h1>连接ftp</h1>
|
||||
* @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<ChannelSftp.LsEntry> v = listFiles(directory, sftp);
|
||||
Iterator<ChannelSftp.LsEntry> 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<ChannelSftp.LsEntry> 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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
serverName=ecology
|
||||
rootPath=F:\wxr\e9-project-ebu7-dev1\src\main\resources\resources
|
||||
systemFilePath=F:\wxr\e9-project-ebu7-dev1\src\main\resources\file
|
||||
logPath=F:\wxr\e9-project-ebu7-dev1\src\main\resources\log
|
|
@ -1,6 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
|
||||
<beans>
|
||||
|
||||
|
||||
<bean id="propertyPlaceholderConfigurer"
|
||||
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
|
||||
<property name="locations" value="classpath:application.properties" />
|
||||
</bean>
|
||||
<bean id="basetest" class="basetest.BaseTestConfig">
|
||||
<property name="serverName" value="${serverName}"/>
|
||||
<property name="rootPath" value="${rootPath}"/>
|
||||
<property name="systemFilePath" value="${systemFilePath}"/>
|
||||
<property name="logPath" value="${logPath}"/>
|
||||
</bean>
|
||||
</beans>
|
|
@ -1,10 +0,0 @@
|
|||
package xuanran.wang.traffic_bank;
|
||||
|
||||
/**
|
||||
* <h1></h1>
|
||||
*
|
||||
* @Author xuanran.wang
|
||||
* @Date 2022/11/22 16:50
|
||||
*/
|
||||
public class TrafficBankTest {
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package xuanran.wang.traffic_bank.waco_first;
|
||||
|
||||
import basetest.BaseTest;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
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.util.ArrayList;
|
||||
|
||||
/**
|
||||
* <h1>威科先行测试类</h1>
|
||||
*
|
||||
* @Author xuanran.wang
|
||||
* @Date 2022/11/22 16:50
|
||||
*/
|
||||
public class WacoFirstTest extends BaseTest {
|
||||
|
||||
private WacoDataPushOAService wacoDataPushOAService = new WacoDataPushOAService();
|
||||
/**
|
||||
* <h1>测试解析xml</h1>
|
||||
* @author xuanran.wang
|
||||
* @dateTime 2022/11/22 21:22
|
||||
**/
|
||||
@Test
|
||||
public void testParseXml() throws Exception {
|
||||
ArrayList<Info> 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"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue