人工交互模块ftp服务上传文件方法修改
人工交互保存数据库方法保存文件修改
This commit is contained in:
parent
ec24147e3f
commit
bc2b3ca4f2
|
@ -281,14 +281,14 @@ public class FTPUtil {
|
||||||
/**
|
/**
|
||||||
* 写入文件,若文件或文件目录不存在则自行创建
|
* 写入文件,若文件或文件目录不存在则自行创建
|
||||||
* @param filePath 文件路径
|
* @param filePath 文件路径
|
||||||
* @param fileName 文件名称
|
|
||||||
* @param inputStream 文件输入流
|
* @param inputStream 文件输入流
|
||||||
* @return 返回值true/false
|
* @return 返回值true/false
|
||||||
*/
|
*/
|
||||||
public synchronized boolean saveFile(String filePath, InputStream inputStream){
|
public synchronized boolean saveFile(String filePath, InputStream inputStream){
|
||||||
//声明目标文件
|
//声明目标文件
|
||||||
File targetFile = new File(filePath);
|
File targetFile = new File(filePath);
|
||||||
OutputStream outputStream = null;
|
//创建输出流
|
||||||
|
BufferedOutputStream outputStream = null;
|
||||||
try {
|
try {
|
||||||
//获取父级路径
|
//获取父级路径
|
||||||
File directory = targetFile.getParentFile();
|
File directory = targetFile.getParentFile();
|
||||||
|
@ -297,7 +297,7 @@ public class FTPUtil {
|
||||||
directory.mkdirs();
|
directory.mkdirs();
|
||||||
}
|
}
|
||||||
// 创建输出流对象并写入数据到文件
|
// 创建输出流对象并写入数据到文件
|
||||||
outputStream = new FileOutputStream(targetFile);
|
outputStream = new BufferedOutputStream(new FileOutputStream(targetFile));
|
||||||
byte[] buffer = new byte[1024];
|
byte[] buffer = new byte[1024];
|
||||||
int length;
|
int length;
|
||||||
while ((length = inputStream.read(buffer)) > 0) {
|
while ((length = inputStream.read(buffer)) > 0) {
|
||||||
|
|
|
@ -4995,40 +4995,40 @@ public class GammaServiceImpl extends AbstractLogOrReport implements IGammaServi
|
||||||
phd.setStatus("R");
|
phd.setStatus("R");
|
||||||
//分析成功后存储日志文件和报告文件
|
//分析成功后存储日志文件和报告文件
|
||||||
String rootPath = spectrumPathProperties.getRootPath();
|
String rootPath = spectrumPathProperties.getRootPath();
|
||||||
{
|
// {
|
||||||
File baselineFile = new File(rootPath+middleData.analyses_baseline_absolute_filePath);
|
// File baselineFile = new File(rootPath+middleData.analyses_baseline_absolute_filePath);
|
||||||
try {
|
// try {
|
||||||
FileInputStream in = new FileInputStream(baselineFile);
|
// FileInputStream in = new FileInputStream(baselineFile);
|
||||||
ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_baseline_absolute_filePath, in);
|
// ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_baseline_absolute_filePath, in);
|
||||||
} catch (FileNotFoundException e) {
|
// } catch (FileNotFoundException e) {
|
||||||
throw new RuntimeException(e);
|
// throw new RuntimeException(e);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
{
|
// {
|
||||||
File lcFile = new File(rootPath+middleData.analyses_lc_absolute_filePath);
|
// File lcFile = new File(rootPath+middleData.analyses_lc_absolute_filePath);
|
||||||
try {
|
// try {
|
||||||
FileInputStream in = new FileInputStream(lcFile);
|
// FileInputStream in = new FileInputStream(lcFile);
|
||||||
ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_lc_absolute_filePath, in);
|
// ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_lc_absolute_filePath, in);
|
||||||
} catch (FileNotFoundException e) {
|
// } catch (FileNotFoundException e) {
|
||||||
throw new RuntimeException(e);
|
// throw new RuntimeException(e);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
{
|
// {
|
||||||
File scacFile = new File(rootPath+middleData.analyses_scac_absolute_filePath);
|
// File scacFile = new File(rootPath+middleData.analyses_scac_absolute_filePath);
|
||||||
try {
|
// try {
|
||||||
FileInputStream in = new FileInputStream(scacFile);
|
// FileInputStream in = new FileInputStream(scacFile);
|
||||||
ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_scac_absolute_filePath, in);
|
// ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_scac_absolute_filePath, in);
|
||||||
} catch (FileNotFoundException e) {
|
// } catch (FileNotFoundException e) {
|
||||||
throw new RuntimeException(e);
|
// throw new RuntimeException(e);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
{
|
{
|
||||||
String logFileName = middleData.analyses_absolute_LogPath.substring(middleData.analyses_absolute_LogPath.lastIndexOf(StringPool.SLASH)+1);
|
String logFileName = middleData.analyses_absolute_LogPath.substring(middleData.analyses_absolute_LogPath.lastIndexOf(StringPool.SLASH)+1);
|
||||||
File logFile = new File(logFileName);
|
File logFile = new File(logFileName);
|
||||||
try {
|
try {
|
||||||
FileUtil.writeString(gammaFileUtil.GetLogContent(middleData), logFile, "UTF-8");
|
FileUtil.writeString(gammaFileUtil.GetLogContent(middleData), logFile, "UTF-8");
|
||||||
FileInputStream in = new FileInputStream(logFile);
|
FileInputStream in = new FileInputStream(logFile);
|
||||||
ftpUtil.saveFile(middleData.analyses_absolute_LogPath, in);
|
ftpUtil.saveFile(ftpUtil.getFtpRootPath()+middleData.analyses_absolute_LogPath, in);
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -5041,7 +5041,7 @@ public class GammaServiceImpl extends AbstractLogOrReport implements IGammaServi
|
||||||
try {
|
try {
|
||||||
FileUtil.writeString(gammaFileUtil.GetReportContent(middleData), rptFile, "UTF-8");
|
FileUtil.writeString(gammaFileUtil.GetReportContent(middleData), rptFile, "UTF-8");
|
||||||
FileInputStream in = new FileInputStream(rptFile);
|
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) {
|
} catch (FileNotFoundException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -4293,7 +4293,7 @@ public class SpectrumAnalysisServiceImpl extends AbstractLogOrReport implements
|
||||||
FileUtil.writeString("", logFile, "UTF-8");
|
FileUtil.writeString("", logFile, "UTF-8");
|
||||||
}
|
}
|
||||||
FileInputStream in = new FileInputStream(logFile);
|
FileInputStream in = new FileInputStream(logFile);
|
||||||
ftpUtil.saveFile(analyses_absolute_LogPath, in);
|
ftpUtil.saveFile(ftpUtil.getFtpRootPath()+analyses_absolute_LogPath, in);
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -4305,7 +4305,7 @@ public class SpectrumAnalysisServiceImpl extends AbstractLogOrReport implements
|
||||||
try {
|
try {
|
||||||
FileUtil.writeString(rptContent, rptFile, "UTF-8");
|
FileUtil.writeString(rptContent, rptFile, "UTF-8");
|
||||||
FileInputStream in = new FileInputStream(rptFile);
|
FileInputStream in = new FileInputStream(rptFile);
|
||||||
ftpUtil.saveFile(analyses_absolute_ReportPath+".txt", in);
|
ftpUtil.saveFile(ftpUtil.getFtpRootPath()+analyses_absolute_ReportPath+".txt", in);
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -53,31 +53,49 @@ public class SpectrumFileServiceImpl implements ISpectrumFileService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Result<?> upload(MultipartFile file) {
|
public Result<?> upload(MultipartFile file) {
|
||||||
|
//压缩包文件名称
|
||||||
String filename = file.getOriginalFilename();
|
String filename = file.getOriginalFilename();
|
||||||
|
//判断是否是压缩包文件
|
||||||
boolean isZip = filename.endsWith(FileTypeEnum.zip.getType());
|
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();
|
LoginUser user = (LoginUser)SecurityUtils.getSubject().getPrincipal();
|
||||||
String username = user.getUsername();
|
String username = user.getUsername();
|
||||||
FTPClient ftpClient = null;
|
// FTPClient ftpClient = null;
|
||||||
|
//获取文件输出流
|
||||||
FileOutputStream fos = null;
|
FileOutputStream fos = null;
|
||||||
|
//获取压缩包文件输入流
|
||||||
ZipInputStream zipInputStream = null;
|
ZipInputStream zipInputStream = null;
|
||||||
String slash = SymbolConstant.SINGLE_SLASH;
|
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;
|
String tempFilePath = System.getProperty("java.io.tmpdir") + username + slash;
|
||||||
|
//文件名称集合
|
||||||
List<String> fileNames = new ArrayList<>();
|
List<String> fileNames = new ArrayList<>();
|
||||||
|
//文件集合
|
||||||
List<File> fileList = 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";
|
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);
|
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";
|
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);
|
Pattern regexPattern1 = Pattern.compile(sampleRx1);
|
||||||
try{
|
try{
|
||||||
|
//创建本地临时文件夹
|
||||||
File tempDir = new File(tempFilePath);
|
File tempDir = new File(tempFilePath);
|
||||||
if (!tempDir.exists()) tempDir.mkdir();
|
//判断本地临时文件夹是否存在
|
||||||
|
if (!tempDir.exists()) {
|
||||||
|
tempDir.mkdir();
|
||||||
|
}
|
||||||
|
//创建输入流
|
||||||
zipInputStream = new ZipInputStream(file.getInputStream());
|
zipInputStream = new ZipInputStream(file.getInputStream());
|
||||||
ZipEntry entry;
|
ZipEntry entry;
|
||||||
|
//遍历获取压缩包内文件
|
||||||
while (ObjectUtil.isNotNull(entry = zipInputStream.getNextEntry())) {
|
while (ObjectUtil.isNotNull(entry = zipInputStream.getNextEntry())) {
|
||||||
|
//文件名称
|
||||||
String fileName = entry.getName();
|
String fileName = entry.getName();
|
||||||
fileNames.add(fileName);
|
fileNames.add(fileName);
|
||||||
File oneFile = new File(tempFilePath + fileName);
|
File oneFile = new File(tempFilePath + fileName);
|
||||||
|
@ -89,46 +107,61 @@ public class SpectrumFileServiceImpl implements ISpectrumFileService {
|
||||||
}
|
}
|
||||||
fileList.add(oneFile);
|
fileList.add(oneFile);
|
||||||
}
|
}
|
||||||
if (CollUtil.isEmpty(fileList))
|
//判断文件集合是否为空
|
||||||
|
if (CollUtil.isEmpty(fileList)) {
|
||||||
return Result.error(Prompt.FILE_IS_EMPTY);
|
return Result.error(Prompt.FILE_IS_EMPTY);
|
||||||
ftpClient = ftpUtil.LoginFTP();
|
}
|
||||||
if (ObjectUtil.isNull(ftpClient))
|
// //登陆ftp
|
||||||
return Result.error(Prompt.FTP_ERR);
|
// ftpClient = ftpUtil.LoginFTP();
|
||||||
// 如果指定目录不存在,逐级创建目录
|
// if (ObjectUtil.isNull(ftpClient)) {
|
||||||
boolean created = FTPUtil.createDirs(ftpClient, filePath);
|
// return Result.error(Prompt.FTP_ERR);
|
||||||
if (!created) return Result.error(Prompt.DIR_CREATE_FAIL + filePath);
|
// }
|
||||||
|
// // 如果指定目录不存在,逐级创建目录
|
||||||
|
// boolean created = FTPUtil.createDirs(ftpClient, filePath);
|
||||||
|
// if (!created) {
|
||||||
|
// return Result.error(Prompt.DIR_CREATE_FAIL + filePath);
|
||||||
|
// }
|
||||||
// 上传所有文件
|
// 上传所有文件
|
||||||
List<String> failList = new ArrayList<>();
|
List<String> failList = new ArrayList<>();
|
||||||
for (File oneFile : fileList) {
|
for (File oneFile : fileList) {
|
||||||
String fileName = oneFile.getName();
|
String fileName = oneFile.getName();
|
||||||
// 判断能谱文件名称是否符合规则,不符合则进行重命名
|
// 判断能谱文件名称是否符合规则,不符合则进行重命名
|
||||||
if (!regexPattern.matcher(fileName).find() && !regexPattern1.matcher(fileName).find()) {
|
if (!regexPattern.matcher(fileName).find() && !regexPattern1.matcher(fileName).find()) {
|
||||||
|
//分析文件
|
||||||
EnergySpectrumStruct struct = phdFileUtil.analyzeFileSourceData(oneFile);
|
EnergySpectrumStruct struct = phdFileUtil.analyzeFileSourceData(oneFile);
|
||||||
|
//获取文件后缀
|
||||||
String suffix = nameStandUtil.GetSuffix(struct.data_type, struct.spectrum_quantity, String.valueOf(struct.acquisition_live_time));
|
String suffix = nameStandUtil.GetSuffix(struct.data_type, struct.spectrum_quantity, String.valueOf(struct.acquisition_live_time));
|
||||||
|
//获取文件名称
|
||||||
fileName = nameStandUtil.GetFileNameFromDateTime(struct.measurement_id, suffix);
|
fileName = nameStandUtil.GetFileNameFromDateTime(struct.measurement_id, suffix);
|
||||||
}
|
}
|
||||||
String fullFilePath = filePath + slash + fileName;
|
String fullFilePath = filePath + slash + fileName;
|
||||||
FileInputStream local = new FileInputStream(oneFile);
|
FileInputStream local = new FileInputStream(oneFile);
|
||||||
boolean success = ftpClient.storeFile(fileName, local);
|
boolean success = ftpUtil.saveFile(fullFilePath, local);
|
||||||
if (!success) failList.add(fullFilePath);
|
if (!success) {
|
||||||
|
failList.add(fullFilePath);
|
||||||
}
|
}
|
||||||
if (CollUtil.isNotEmpty(failList))
|
}
|
||||||
|
if (CollUtil.isNotEmpty(failList)) {
|
||||||
return Result.error(Prompt.UPLOAD_ERR, failList);
|
return Result.error(Prompt.UPLOAD_ERR, failList);
|
||||||
|
}
|
||||||
return Result.OK(Prompt.UPLOAD_SUCC);
|
return Result.OK(Prompt.UPLOAD_SUCC);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return Result.error(Prompt.UPLOAD_ERR);
|
return Result.error(Prompt.UPLOAD_ERR);
|
||||||
}finally {
|
}finally {
|
||||||
try {
|
try {
|
||||||
if (ObjectUtil.isNotNull(zipInputStream))
|
if (ObjectUtil.isNotNull(zipInputStream)) {
|
||||||
zipInputStream.close();
|
zipInputStream.close();
|
||||||
if (ObjectUtil.isNotNull(fos))
|
|
||||||
fos.close();
|
|
||||||
if (ObjectUtil.isNotNull(ftpClient))
|
|
||||||
if (ftpClient.isConnected()){
|
|
||||||
ftpClient.logout();
|
|
||||||
ftpClient.disconnect();
|
|
||||||
}
|
}
|
||||||
|
if (ObjectUtil.isNotNull(fos)) {
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
// if (ObjectUtil.isNotNull(ftpClient)) {
|
||||||
|
// if (ftpClient.isConnected()){
|
||||||
|
// ftpClient.logout();
|
||||||
|
// ftpClient.disconnect();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user