feat:整合FtpDownFile

This commit is contained in:
nieziyan 2023-11-01 14:29:12 +08:00
parent 7ed2207730
commit f4cdad297e
14 changed files with 305 additions and 236 deletions

View File

@ -1,7 +1,9 @@
package org.jeecg.common.util; package org.jeecg.common.util;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClient;
@ -13,6 +15,7 @@ import org.springframework.stereotype.Component;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -303,4 +306,60 @@ public class FTPUtil {
} }
return true; return true;
} }
public File fTPFile(String fromPath, String toPath) {
FTPClient ftpClient = null;
InputStream inputStream = null;
try {
ftpClient = LoginFTP();
// 切换被动模式
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 设置编码当文件中存在中文且上传后文件乱码时可使用此配置项
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
inputStream = ftpClient.retrieveFileStream(fromPath);
// 声明一个临时文件
File tempFile = File.createTempFile(toPath, null);
// 将FTP文件的输入流复制给临时文件
FileUtils.copyInputStreamToFile(inputStream, tempFile);
return tempFile;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
try {
if (ObjectUtil.isNotNull(ftpClient))
ftpClient.disconnect();
if (ObjectUtil.isNotNull(inputStream))
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public InputStream fTPFileStream(String fromPath) {
FTPClient ftpClient = null;
try {
ftpClient = LoginFTP();
// 切换被动模式
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 设置编码当文件中存在中文且上传后文件乱码时可使用此配置项
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
return ftpClient.retrieveFileStream(fromPath);
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
try {
if (ObjectUtil.isNotNull(ftpClient))
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} }

View File

@ -0,0 +1,25 @@
package org.jeecg.config.mybatis;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
/**
* 数据源切换器
*/
public class DSSwitcher {
private final static String ORACLE = "ora";
private final static String PGSQL = "master";
public static void switchToOracle(){
DynamicDataSourceContextHolder.push(ORACLE);
}
public static void switchToPGSql(){
DynamicDataSourceContextHolder.push(PGSQL);
}
public static void clear(){
DynamicDataSourceContextHolder.clear();
}
}

View File

@ -63,4 +63,10 @@ public class SysDatabaseController {
List<SourceDto> sourceDtos = sysDatabaseService.listAll(); List<SourceDto> sourceDtos = sysDatabaseService.listAll();
return Result.OK(sourceDtos); return Result.OK(sourceDtos);
} }
@GetMapping("dbNames")
@ApiOperation(value = "数据库名列表",notes = "数据库名列表")
public Result<?> dbNames(@RequestParam String dbType){
return Result.OK(sysDatabaseService.dbNames(dbType));
}
} }

View File

@ -13,4 +13,10 @@ public interface SysDatabaseMapper extends BaseMapper<SysDatabase> {
List<AlarmHistory> findAlarmHistory(Map<String,Object> params); List<AlarmHistory> findAlarmHistory(Map<String,Object> params);
List<DatabaseDto> findPage(Map<String,Object> params); List<DatabaseDto> findPage(Map<String,Object> params);
List<String> dbNamesPG();
List<String> dbNamesMY();
List<String> dbNamesOR();
} }

View File

@ -18,10 +18,4 @@ public interface SysServerMapper extends BaseMapper<SysServer> {
List<SourceDto> pageAll(String itemName); List<SourceDto> pageAll(String itemName);
List<AlarmInfo> alarmInfo(String sourceId); List<AlarmInfo> alarmInfo(String sourceId);
List<String> dbNamesPG();
List<String> dbNamesMY();
List<String> dbNamesOR();
} }

View File

@ -72,14 +72,4 @@
WHERE r.source_id = #{sourceId} WHERE r.source_id = #{sourceId}
ORDER BY l.alarm_start_date DESC ORDER BY l.alarm_start_date DESC
</select> </select>
<select id="dbNamesPG" resultType="java.lang.String">
SELECT datname FROM pg_database WHERE datistemplate = false;
</select>
<select id="dbNamesMY" resultType="java.lang.String">
SHOW DATABASES;
</select>
<select id="dbNamesOR" resultType="java.lang.String">
SELECT username FROM all_users;
</select>
</mapper> </mapper>

View File

@ -50,5 +50,13 @@
LIMIT #{pageSize} OFFSET #{pageStart} LIMIT #{pageSize} OFFSET #{pageStart}
</if> </if>
</select> </select>
<select id="dbNamesPG" resultType="java.lang.String">
SELECT datname FROM pg_database WHERE datistemplate = false;
</select>
<select id="dbNamesMY" resultType="java.lang.String">
SHOW DATABASES;
</select>
<select id="dbNamesOR" resultType="java.lang.String">
SELECT username FROM all_users;
</select>
</mapper> </mapper>

View File

@ -24,4 +24,6 @@ public interface ISysDatabaseService extends IService<SysDatabase> {
Result findAlarmHistory(SourceVo sourceVo); Result findAlarmHistory(SourceVo sourceVo);
List<SourceDto> listAll(); List<SourceDto> listAll();
List<String> dbNames(String dbType);
} }

View File

@ -33,6 +33,4 @@ public interface ISysServerService extends IService<SysServer> {
Result<?> details_BasicInfo(String hostId); Result<?> details_BasicInfo(String hostId);
Result<?> details_AlarmInfo(String sourceId, Integer pageNo, Integer pageSize); Result<?> details_AlarmInfo(String sourceId, Integer pageNo, Integer pageSize);
List<String> dbNames();
} }

View File

@ -9,10 +9,12 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.QueryRequest; import org.jeecg.common.api.QueryRequest;
import org.jeecg.common.api.vo.Result; import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.DataBaseConstant;
import org.jeecg.common.constant.DateConstant; import org.jeecg.common.constant.DateConstant;
import org.jeecg.common.constant.DictConstant; import org.jeecg.common.constant.DictConstant;
import org.jeecg.common.constant.Prompt; import org.jeecg.common.constant.Prompt;
import org.jeecg.common.system.vo.DictModel; import org.jeecg.common.system.vo.DictModel;
import org.jeecg.config.mybatis.DSSwitcher;
import org.jeecg.modules.base.dto.DatabaseDto; import org.jeecg.modules.base.dto.DatabaseDto;
import org.jeecg.modules.base.dto.SourceDto; import org.jeecg.modules.base.dto.SourceDto;
import org.jeecg.modules.base.entity.postgre.SysDatabase; import org.jeecg.modules.base.entity.postgre.SysDatabase;
@ -178,4 +180,25 @@ public class SysDatabaseServiceImpl extends ServiceImpl<SysDatabaseMapper, SysDa
return sourceDtos; return sourceDtos;
} }
@Override
public List<String> dbNames(String dbType) {
List<String> dbNames = new ArrayList<>();
switch (dbType){
case DataBaseConstant.DB_TYPE_ORACLE:
DSSwitcher.switchToOracle();
dbNames = baseMapper.dbNamesOR();
DSSwitcher.clear();
break;
case DataBaseConstant.DB_TYPE_POSTGRESQL:
dbNames = baseMapper.dbNamesPG();
break;
case DataBaseConstant.DB_TYPE_MYSQL:
// ...
break;
default:
break;
}
return dbNames;
}
} }

View File

@ -304,10 +304,4 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
page.setRecords(records); page.setRecords(records);
return Result.OK(page); return Result.OK(page);
} }
@Override
public List<String> dbNames() {
return null;
}
} }

View File

@ -18,11 +18,8 @@ import org.jeecg.common.constant.*;
import org.jeecg.common.constant.enums.SpectrumSystemType; import org.jeecg.common.constant.enums.SpectrumSystemType;
import org.jeecg.common.properties.ParameterProperties; import org.jeecg.common.properties.ParameterProperties;
import org.jeecg.common.properties.SpectrumPathProperties; import org.jeecg.common.properties.SpectrumPathProperties;
import org.jeecg.common.util.DateUtils; import org.jeecg.common.util.*;
import org.jeecg.common.util.GammaFileUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.base.bizVo.AttributeItemVo; import org.jeecg.modules.base.bizVo.AttributeItemVo;
import org.jeecg.common.util.MyLogFormatUtil;
import org.jeecg.modules.base.dto.*; import org.jeecg.modules.base.dto.*;
import org.jeecg.modules.base.entity.original.GardsSampleData; import org.jeecg.modules.base.entity.original.GardsSampleData;
import org.jeecg.modules.base.entity.rnauto.*; import org.jeecg.modules.base.entity.rnauto.*;
@ -61,6 +58,8 @@ public class Sample_G_Analysis {
private RedisUtil redisUtil; private RedisUtil redisUtil;
private FTPUtil ftpUtil;
/** /**
* 系统类型 * 系统类型
*/ */
@ -124,9 +123,10 @@ public class Sample_G_Analysis {
PHDFile phdFile = new PHDFile(); PHDFile phdFile = new PHDFile();
phdFile.setXmlFilePath(parameterProperties.getFilePath()); phdFile.setXmlFilePath(parameterProperties.getFilePath());
// 解析PHD文件 // 解析PHD文件
spectrumPathProperties = ApplicationContextUtil.getContext().getBean(SpectrumPathProperties.class); spectrumPathProperties = SpringContextUtils.getBean(SpectrumPathProperties.class);
ftpUtil = SpringContextUtils.getBean(FTPUtil.class);
String sampleFilePath = sampleData.getInputFileName(); String sampleFilePath = sampleData.getInputFileName();
String pathName = File.separator + spectrumPathProperties.getSaveFilePath() + File.separator + String pathName = ftpUtil.getFtpRootPath() + spectrumPathProperties.getSaveFilePath() + File.separator +
sampleFilePath.substring(0, sampleFilePath.lastIndexOf(StringPool.SLASH)); sampleFilePath.substring(0, sampleFilePath.lastIndexOf(StringPool.SLASH));
String fileName = sampleFilePath.substring(sampleFilePath.lastIndexOf(StringPool.SLASH)+1); String fileName = sampleFilePath.substring(sampleFilePath.lastIndexOf(StringPool.SLASH)+1);

View File

@ -11,7 +11,6 @@ import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClient;
import org.checkerframework.checker.units.qual.N;
import org.ejml.simple.SimpleMatrix; import org.ejml.simple.SimpleMatrix;
import org.jeecg.common.api.vo.Result; import org.jeecg.common.api.vo.Result;
import org.jeecg.common.properties.ParameterProperties; import org.jeecg.common.properties.ParameterProperties;
@ -30,7 +29,6 @@ import org.jeecg.modules.native_jni.struct.EnergySpectrumStruct;
import org.jeecgframework.core.util.ApplicationContextUtil; import org.jeecgframework.core.util.ApplicationContextUtil;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.w3c.dom.*; import org.w3c.dom.*;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
@ -41,8 +39,6 @@ import javax.xml.parsers.ParserConfigurationException;
import java.io.*; import java.io.*;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException; import java.text.ParseException;
import java.util.*; import java.util.*;
@ -64,28 +60,9 @@ public class GammaFileUtil extends AbstractLogOrReport {
public boolean loadFile(String pathName, String fileName, PHDFile phd, Result result) { public boolean loadFile(String pathName, String fileName, PHDFile phd, Result result) {
phd.setFilepath(pathName); phd.setFilepath(pathName);
phd.setFilename(fileName); phd.setFilename(fileName);
//连接ftp String fromPath = pathName + File.separator + fileName;
FTPClient ftpClient = ftpUtil.LoginFTP(); File file = ftpUtil.fTPFile(fromPath, "betaGamma");
if (Objects.isNull(ftpClient)){
result.error500("ftp connection failed");
return false;
}
InputStream inputStream = null;
File file = null;
try { try {
//切换被动模式
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 设置编码当文件中存在中文且上传后文件乱码时可使用此配置项
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
ftpClient.changeWorkingDirectory(pathName);
inputStream = ftpClient.retrieveFileStream(fileName);
if (Objects.nonNull(inputStream)) {
//声明一个临时文件
file = File.createTempFile("betaGamma", null);
//将ftp文件的输入流复制给临时文件
FileUtils.copyInputStreamToFile(inputStream, file);
//读取文件信息 //读取文件信息
EnergySpectrumStruct struct = EnergySpectrumHandler.getSourceData(file.getAbsolutePath()); EnergySpectrumStruct struct = EnergySpectrumHandler.getSourceData(file.getAbsolutePath());
//MsgInfo //MsgInfo
@ -248,26 +225,13 @@ public class GammaFileUtil extends AbstractLogOrReport {
phd.setBAnalyed(false); phd.setBAnalyed(false);
phd.setAnaly_start_time(DateUtils.formatDate(new Date(), "yyyy/MM/dd HH:mm:ss")); phd.setAnaly_start_time(DateUtils.formatDate(new Date(), "yyyy/MM/dd HH:mm:ss"));
}
}catch (IOException e) { }catch (ParseException e) {
throw new RuntimeException(e);
} catch (ParseException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
try { if (Objects.nonNull(file))
if (Objects.nonNull(ftpClient)){
ftpClient.disconnect();
}
if (Objects.nonNull(inputStream)){
inputStream.close();
}
if (Objects.nonNull(file)) {
file.delete(); file.delete();
} }
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return true; return true;
} }

View File

@ -151,7 +151,7 @@ public class GammaServiceImpl extends AbstractLogOrReport implements IGammaServi
result.error500("Sample file does not exist!"); result.error500("Sample file does not exist!");
return result; return result;
} }
String pathName = StringPool.SLASH + spectrumPathProperties.getSaveFilePath() + StringPool.SLASH + sampleFilePath.substring(0, sampleFilePath.lastIndexOf(StringPool.SLASH)); String pathName = ftpUtil.getFtpRootPath() + spectrumPathProperties.getSaveFilePath() + StringPool.SLASH + sampleFilePath.substring(0, sampleFilePath.lastIndexOf(StringPool.SLASH));
String fileName = sampleFilePath.substring(sampleFilePath.lastIndexOf(StringPool.SLASH) + 1); String fileName = sampleFilePath.substring(sampleFilePath.lastIndexOf(StringPool.SLASH) + 1);
boolean flag = gammaFileUtil.loadFile(pathName, fileName, phd, result); boolean flag = gammaFileUtil.loadFile(pathName, fileName, phd, result);
if (!flag) { if (!flag) {
@ -166,7 +166,7 @@ public class GammaServiceImpl extends AbstractLogOrReport implements IGammaServi
} }
lastName = fileName; lastName = fileName;
} else { } else {
String pathName = StringPool.SLASH + spectrumPathProperties.getUploadPath() + StringPool.SLASH + userName; String pathName = ftpUtil.getFtpRootPath() + spectrumPathProperties.getUploadPath() + StringPool.SLASH + userName;
String fileName = samfileName; String fileName = samfileName;
boolean flag = gammaFileUtil.loadFile(pathName, fileName, phd, result); boolean flag = gammaFileUtil.loadFile(pathName, fileName, phd, result);
if (!flag) { if (!flag) {
@ -425,7 +425,7 @@ public class GammaServiceImpl extends AbstractLogOrReport implements IGammaServi
return result; return result;
} }
// 切割数据库存储的文件路径获取路径信息 // 切割数据库存储的文件路径获取路径信息
String pathName = StringPool.SLASH + spectrumPathProperties.getSaveFilePath() + StringPool.SLASH + sampleFilePath.substring(0, sampleFilePath.lastIndexOf(StringPool.SLASH)); String pathName = ftpUtil.getFtpRootPath() + spectrumPathProperties.getSaveFilePath() + StringPool.SLASH + sampleFilePath.substring(0, sampleFilePath.lastIndexOf(StringPool.SLASH));
// 切割数据库存储的文件路径获取文件名称 // 切割数据库存储的文件路径获取文件名称
String fileName = sampleFilePath.substring(sampleFilePath.lastIndexOf(StringPool.SLASH) + 1); String fileName = sampleFilePath.substring(sampleFilePath.lastIndexOf(StringPool.SLASH) + 1);
// 声明phd实体类 // 声明phd实体类
@ -776,7 +776,7 @@ public class GammaServiceImpl extends AbstractLogOrReport implements IGammaServi
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
Cache<String, PHDFile> phdCache = localCache.getPHDCache(); Cache<String, PHDFile> phdCache = localCache.getPHDCache();
// 上传文件路径 // 上传文件路径
String path = StringPool.SLASH + spectrumPathProperties.getUploadPath() + StringPool.SLASH + userName; String path = ftpUtil.getFtpRootPath() + spectrumPathProperties.getUploadPath() + StringPool.SLASH + userName;
// 获取当前角色的颜色配置 // 获取当前角色的颜色配置
Map<String, String> colorMap = sysUserColorService.initColor(userName); Map<String, String> colorMap = sysUserColorService.initColor(userName);
PHDFile phd = phdCache.getIfPresent(fileName + "-" + userName); PHDFile phd = phdCache.getIfPresent(fileName + "-" + userName);