修改BaseTest,修复全局变量引用log报空指针异常

main
youHong.ai 2022-11-23 14:31:16 +08:00
commit 618d1b2a3d
11 changed files with 415 additions and 21 deletions

2
.gitignore vendored
View File

@ -31,5 +31,7 @@ log
/src/test/resources/application.properties /src/test/resources/application.properties
/src/test/resources/application.xml /src/test/resources/application.xml
DirectoryV2.xml

View File

@ -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>

View File

@ -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 {
}

View File

@ -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;
}

View File

@ -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 aiyh.utils.Util;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -6,6 +6,7 @@ import weaver.interfaces.schedule.BaseCronJob;
/** /**
* <h1></h1> * <h1></h1>
* <p>sftp</p>
* @Author xuanran.wang * @Author xuanran.wang
* @Date 2022/11/22 16:18 * @Date 2022/11/22 16:18
*/ */
@ -15,6 +16,6 @@ public class WacoDataPushOA extends BaseCronJob {
@Override @Override
public void execute() { public void execute() {
} }
} }

View File

@ -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;
}
}

View File

@ -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();
}
}
}

View File

@ -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

View File

@ -1,6 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <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> </beans>

View File

@ -1,10 +0,0 @@
package xuanran.wang.traffic_bank;
/**
* <h1></h1>
*
* @Author xuanran.wang
* @Date 2022/11/22 16:50
*/
public class TrafficBankTest {
}

View File

@ -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"));
}
}