人工交互模块ftp服务上传文件方法修改

人工交互保存数据库方法保存文件修改
This commit is contained in:
qiaoqinzheng 2024-03-13 16:37:48 +08:00
parent ec24147e3f
commit bc2b3ca4f2
4 changed files with 88 additions and 55 deletions

View File

@ -281,14 +281,14 @@ public class FTPUtil {
/**
* 写入文件若文件或文件目录不存在则自行创建
* @param filePath 文件路径
* @param fileName 文件名称
* @param inputStream 文件输入流
* @return 返回值true/false
*/
public synchronized boolean saveFile(String filePath, InputStream inputStream){
//声明目标文件
File targetFile = new File(filePath);
OutputStream outputStream = null;
//创建输出流
BufferedOutputStream outputStream = null;
try {
//获取父级路径
File directory = targetFile.getParentFile();
@ -297,7 +297,7 @@ public class FTPUtil {
directory.mkdirs();
}
// 创建输出流对象并写入数据到文件
outputStream = new FileOutputStream(targetFile);
outputStream = new BufferedOutputStream(new FileOutputStream(targetFile));
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {

View File

@ -4995,40 +4995,40 @@ public class GammaServiceImpl extends AbstractLogOrReport implements IGammaServi
phd.setStatus("R");
//分析成功后存储日志文件和报告文件
String rootPath = spectrumPathProperties.getRootPath();
{
File baselineFile = new File(rootPath+middleData.analyses_baseline_absolute_filePath);
try {
FileInputStream in = new FileInputStream(baselineFile);
ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_baseline_absolute_filePath, in);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
{
File lcFile = new File(rootPath+middleData.analyses_lc_absolute_filePath);
try {
FileInputStream in = new FileInputStream(lcFile);
ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_lc_absolute_filePath, in);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
{
File scacFile = new File(rootPath+middleData.analyses_scac_absolute_filePath);
try {
FileInputStream in = new FileInputStream(scacFile);
ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_scac_absolute_filePath, in);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
// {
// File baselineFile = new File(rootPath+middleData.analyses_baseline_absolute_filePath);
// try {
// FileInputStream in = new FileInputStream(baselineFile);
// ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_baseline_absolute_filePath, in);
// } catch (FileNotFoundException e) {
// throw new RuntimeException(e);
// }
// }
// {
// File lcFile = new File(rootPath+middleData.analyses_lc_absolute_filePath);
// try {
// FileInputStream in = new FileInputStream(lcFile);
// ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_lc_absolute_filePath, in);
// } catch (FileNotFoundException e) {
// throw new RuntimeException(e);
// }
// }
// {
// File scacFile = new File(rootPath+middleData.analyses_scac_absolute_filePath);
// try {
// FileInputStream in = new FileInputStream(scacFile);
// ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_scac_absolute_filePath, in);
// } catch (FileNotFoundException e) {
// throw new RuntimeException(e);
// }
// }
{
String logFileName = middleData.analyses_absolute_LogPath.substring(middleData.analyses_absolute_LogPath.lastIndexOf(StringPool.SLASH)+1);
File logFile = new File(logFileName);
try {
FileUtil.writeString(gammaFileUtil.GetLogContent(middleData), logFile, "UTF-8");
FileInputStream in = new FileInputStream(logFile);
ftpUtil.saveFile(middleData.analyses_absolute_LogPath, in);
ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_absolute_LogPath, in);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} finally {
@ -5041,7 +5041,7 @@ public class GammaServiceImpl extends AbstractLogOrReport implements IGammaServi
try {
FileUtil.writeString(gammaFileUtil.GetReportContent(middleData), rptFile, "UTF-8");
FileInputStream in = new FileInputStream(rptFile);
ftpUtil.saveFile(middleData.analyses_absolute_ReportPath+".txt", in);
ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_absolute_ReportPath+".txt", in);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} finally {

View File

@ -4293,7 +4293,7 @@ public class SpectrumAnalysisServiceImpl extends AbstractLogOrReport implements
FileUtil.writeString("", logFile, "UTF-8");
}
FileInputStream in = new FileInputStream(logFile);
ftpUtil.saveFile(analyses_absolute_LogPath, in);
ftpUtil.saveFile(ftpUtil.getFtpRootPath()+analyses_absolute_LogPath, in);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
@ -4305,7 +4305,7 @@ public class SpectrumAnalysisServiceImpl extends AbstractLogOrReport implements
try {
FileUtil.writeString(rptContent, rptFile, "UTF-8");
FileInputStream in = new FileInputStream(rptFile);
ftpUtil.saveFile(analyses_absolute_ReportPath+".txt", in);
ftpUtil.saveFile(ftpUtil.getFtpRootPath()+analyses_absolute_ReportPath+".txt", in);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} finally {

View File

@ -53,31 +53,49 @@ public class SpectrumFileServiceImpl implements ISpectrumFileService {
@Override
public Result<?> upload(MultipartFile file) {
//压缩包文件名称
String filename = file.getOriginalFilename();
//判断是否是压缩包文件
boolean isZip = filename.endsWith(FileTypeEnum.zip.getType());
if (!isZip) return Result.error(Prompt.FILE_TYPE_ERR);
//如果不是压缩包文件 返回错误提示信息
if (!isZip) {
return Result.error(Prompt.FILE_TYPE_ERR);
}
//获取登陆用户名
LoginUser user = (LoginUser)SecurityUtils.getSubject().getPrincipal();
String username = user.getUsername();
FTPClient ftpClient = null;
// FTPClient ftpClient = null;
//获取文件输出流
FileOutputStream fos = null;
//获取压缩包文件输入流
ZipInputStream zipInputStream = null;
String slash = SymbolConstant.SINGLE_SLASH;
//上传文件夹路径
String filePath = spectrumPathProperties.getUploadPath() + slash + username;
String filePath = ftpUtil.getFtpRootPath() + slash + spectrumPathProperties.getUploadPath() + slash + username;
//本地临时文件夹路径
String tempFilePath = System.getProperty("java.io.tmpdir") + username + slash;
//文件名称集合
List<String> fileNames = new ArrayList<>();
//文件集合
List<File> fileList = new ArrayList<>();
//正则表达式
String sampleRx = "[a-zA-Z]{3}[0-9]{2}_[0-9]{3}-[0-9]{8}_[0-9]{4}_(S|G|D|Q)_(FULL_|PREL_)\\d+\\.PHD";
Pattern regexPattern = Pattern.compile(sampleRx);
String sampleRx1 = "[a-zA-Z]{3}[0-9]{2}_[0-9]{3}-[0-9]{8}_[0-9]{4}_(S|G|D|Q)_(FULL_|PREL_)\\d+\\.\\d+\\.PHD";
Pattern regexPattern1 = Pattern.compile(sampleRx1);
try{
//创建本地临时文件夹
File tempDir = new File(tempFilePath);
if (!tempDir.exists()) tempDir.mkdir();
//判断本地临时文件夹是否存在
if (!tempDir.exists()) {
tempDir.mkdir();
}
//创建输入流
zipInputStream = new ZipInputStream(file.getInputStream());
ZipEntry entry;
//遍历获取压缩包内文件
while (ObjectUtil.isNotNull(entry = zipInputStream.getNextEntry())) {
//文件名称
String fileName = entry.getName();
fileNames.add(fileName);
File oneFile = new File(tempFilePath + fileName);
@ -89,46 +107,61 @@ public class SpectrumFileServiceImpl implements ISpectrumFileService {
}
fileList.add(oneFile);
}
if (CollUtil.isEmpty(fileList))
//判断文件集合是否为空
if (CollUtil.isEmpty(fileList)) {
return Result.error(Prompt.FILE_IS_EMPTY);
ftpClient = ftpUtil.LoginFTP();
if (ObjectUtil.isNull(ftpClient))
return Result.error(Prompt.FTP_ERR);
// 如果指定目录不存在,逐级创建目录
boolean created = FTPUtil.createDirs(ftpClient, filePath);
if (!created) return Result.error(Prompt.DIR_CREATE_FAIL + filePath);
}
// //登陆ftp
// ftpClient = ftpUtil.LoginFTP();
// if (ObjectUtil.isNull(ftpClient)) {
// return Result.error(Prompt.FTP_ERR);
// }
// // 如果指定目录不存在,逐级创建目录
// boolean created = FTPUtil.createDirs(ftpClient, filePath);
// if (!created) {
// return Result.error(Prompt.DIR_CREATE_FAIL + filePath);
// }
// 上传所有文件
List<String> failList = new ArrayList<>();
for (File oneFile : fileList) {
String fileName = oneFile.getName();
// 判断能谱文件名称是否符合规则不符合则进行重命名
if (!regexPattern.matcher(fileName).find() && !regexPattern1.matcher(fileName).find()) {
//分析文件
EnergySpectrumStruct struct = phdFileUtil.analyzeFileSourceData(oneFile);
//获取文件后缀
String suffix = nameStandUtil.GetSuffix(struct.data_type, struct.spectrum_quantity, String.valueOf(struct.acquisition_live_time));
//获取文件名称
fileName = nameStandUtil.GetFileNameFromDateTime(struct.measurement_id, suffix);
}
String fullFilePath = filePath + slash + fileName;
FileInputStream local = new FileInputStream(oneFile);
boolean success = ftpClient.storeFile(fileName, local);
if (!success) failList.add(fullFilePath);
boolean success = ftpUtil.saveFile(fullFilePath, local);
if (!success) {
failList.add(fullFilePath);
}
}
if (CollUtil.isNotEmpty(failList))
if (CollUtil.isNotEmpty(failList)) {
return Result.error(Prompt.UPLOAD_ERR, failList);
}
return Result.OK(Prompt.UPLOAD_SUCC);
} catch (IOException e) {
e.printStackTrace();
return Result.error(Prompt.UPLOAD_ERR);
}finally {
try {
if (ObjectUtil.isNotNull(zipInputStream))
if (ObjectUtil.isNotNull(zipInputStream)) {
zipInputStream.close();
if (ObjectUtil.isNotNull(fos))
}
if (ObjectUtil.isNotNull(fos)) {
fos.close();
if (ObjectUtil.isNotNull(ftpClient))
if (ftpClient.isConnected()){
ftpClient.logout();
ftpClient.disconnect();
}
}
// if (ObjectUtil.isNotNull(ftpClient)) {
// if (ftpClient.isConnected()){
// ftpClient.logout();
// ftpClient.disconnect();
// }
// }
} catch (IOException e) {
e.printStackTrace();
}