方法优化

This commit is contained in:
nieziyan 2023-07-13 08:53:55 +08:00
parent 8928607514
commit 4410bf68d3
2 changed files with 38 additions and 32 deletions

View File

@ -23,9 +23,7 @@ public class PHDFileUtil {
public static Map<String, Object> getSourceData(String filePath){ public static Map<String, Object> getSourceData(String filePath){
//加载dll工具库 //加载dll工具库
/* System.loadLibrary("ReadPHDFile");*/ System.loadLibrary("ReadPHDFile");
String dllPath = "D:\\Work\\C++\\c++lib\\ReadPHDFile.dll";
System.load(dllPath);
EnergySpectrumStruct struct = EnergySpectrumHandler.getSourceData(filePath); EnergySpectrumStruct struct = EnergySpectrumHandler.getSourceData(filePath);
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
try { try {
@ -116,8 +114,8 @@ public class PHDFileUtil {
//Gamma Spectrum Projected //Gamma Spectrum Projected
List<Double> gCentroidChannel = struct.g_centroid_channel; List<Double> gCentroidChannel = struct.g_centroid_channel;
List<Double> gEnergy = struct.g_energy; List<Double> gEnergy = struct.g_energy;
/*List<Double> gammaProjectedData = EnergySpectrumHandler.GetFileFittingPara(gCentroidChannel, gEnergy);*/ List<Double> gammaProjectedData = EnergySpectrumHandler.GetFileFittingPara(gCentroidChannel, gEnergy);
map.put("gammaProjectedData", null); map.put("gammaProjectedData", gammaProjectedData);
//Beta Spectrum Original //Beta Spectrum Original
List<Long> betaOriginalData = new LinkedList<>(); List<Long> betaOriginalData = new LinkedList<>();
@ -141,8 +139,8 @@ public class PHDFileUtil {
//Beta Spectrum Projected //Beta Spectrum Projected
List<Double> bChannel = struct.b_channel; List<Double> bChannel = struct.b_channel;
List<Double> bElectronEnergy = struct.b_electron_energy; List<Double> bElectronEnergy = struct.b_electron_energy;
/*List<Double> betaProjectedData = EnergySpectrumHandler.GetFileFittingPara(bChannel, bElectronEnergy);*/ List<Double> betaProjectedData = EnergySpectrumHandler.GetFileFittingPara(bChannel, bElectronEnergy);
map.put("betaProjectedData", null); map.put("betaProjectedData", betaProjectedData);
} catch (ParseException e) { } catch (ParseException e) {
throw new RuntimeException(e); throw new RuntimeException(e);

View File

@ -1,6 +1,7 @@
package org.jeecg.common.util; package org.jeecg.common.util;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.core.toolkit.StringPool;
@ -9,6 +10,7 @@ import org.apache.commons.io.FileUtils;
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.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPFile;
import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.enums.SampleFileHeader; import org.jeecg.common.enums.SampleFileHeader;
import org.jeecg.modules.entity.data.HistogramData; import org.jeecg.modules.entity.data.HistogramData;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -158,7 +160,7 @@ public class ReadLineUtil {
outputStream.write(buffer, 0, bytesRead); outputStream.write(buffer, 0, bytesRead);
} }
} catch (FileNotFoundException e){ } catch (FileNotFoundException e){
log.error("Report文件["+filePath+"]不存在!"); log.error("文件["+filePath+"]不存在!");
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -178,42 +180,49 @@ public class ReadLineUtil {
FTPClient ftpClient = ftpUtil.LoginFTP(); FTPClient ftpClient = ftpUtil.LoginFTP();
// 判断FTP是否连接成功 // 判断FTP是否连接成功
if (Objects.isNull(ftpClient)){ if (Objects.isNull(ftpClient)){
throw new RuntimeException("ftp连接失败!"); throw new RuntimeException("FTP连接失败!");
} }
String fileName = filePath.substring(filePath.lastIndexOf(StringPool.SLASH) + 1);
String parameterFilePath = filePath.substring(0, filePath.lastIndexOf(StringPool.SLASH));
// 根据字符切割文件路径
List<String> paths = Arrays.asList(parameterFilePath.split(StringPool.SLASH));
OutputStream outputStream = null; OutputStream outputStream = null;
InputStream inputStream = null; InputStream inputStream = null;
try { try {
for (String path : paths) { // 切换工作目录为 /
ftpClient.changeWorkingDirectory(path); ftpClient.changeWorkingDirectory(SymbolConstant.SINGLE_SLASH);
// 判断FTP服务器上是否存在此文件
String[] files = ftpClient.listNames(filePath);
if (ArrayUtil.isEmpty(files)){
log.error("文件["+filePath+"]不存在!");
return;
} }
// 存在多个文件名表示此路径为目录而非文件
if (ArrayUtil.length(files) > 1){
log.error("路径["+filePath+"]存在多个文件名,可能是一个目录!");
return;
}
// 获取文件名
String fileName = FileUtil.getName(filePath);
// 在当前工作路径下读取文件 // 在当前工作路径下读取文件
ftpClient.enterLocalPassiveMode(); ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 设置编码当文件中存在中文且上传后文件乱码时可使用此配置项 // 设置编码当文件中存在中文且上传后文件乱码时可使用此配置项
ftpClient.setControlEncoding(encoding); ftpClient.setControlEncoding(encoding);
ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE); ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
List<FTPFile> ftpFiles = Arrays.asList(ftpClient.listFiles()); inputStream = ftpClient.retrieveFileStream(filePath);
for (FTPFile ftpFile : ftpFiles) { outputStream = response.getOutputStream();
if (ftpFile.getName().equals(fileName)){ // 设置响应头
inputStream = ftpClient.retrieveFileStream(fileName); response.setContentType("application/octet-stream");
outputStream = response.getOutputStream(); response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
// 设置响应头 // 缓冲区大小
response.setContentType("application/octet-stream"); byte[] buffer = new byte[4096];
response.setHeader("Content-Disposition", "attachment; filename=" + fileName); int bytesRead;
// 缓冲区大小
byte[] buffer = new byte[4096];
int bytesRead;
// 将文件输出流写入到输出流中 // 将文件输出流写入到输出流中
while ((bytesRead = inputStream.read(buffer)) != -1) { while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead); outputStream.write(buffer, 0, bytesRead);
}
}
} }
} }
catch (IOException e) { catch (IOException e) {
@ -227,7 +236,6 @@ public class ReadLineUtil {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }