web模块下载zip文件功能其他谱文件下载修改

This commit is contained in:
qiaoqinzheng 2023-11-14 18:06:58 +08:00
parent 93cb64cd07
commit 65c15847b0
2 changed files with 38 additions and 26 deletions

View File

@ -167,9 +167,9 @@ public class FTPUtil {
response.reset();
//设置响应类型
response.setContentType("application/download");
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
//解决中文不能生成文件
response.setHeader("Content-Disposition", "attachment; fileName=" + URLEncoder.encode(fileName,"UTF-8"));
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
//获取输出流
out = response.getOutputStream();
//声明一个长度参数

View File

@ -191,9 +191,11 @@ public class GardsSampleDataWebServiceImpl extends ServiceImpl<GardsSampleDataWe
InputStream detStream = null;
File qcFile = null;
InputStream qcStream = null;
String zipFileName = "";
try {
//根据sampleId查询自动处理库获取相应分析id
Integer analysisID = this.baseMapper.getAnalysisID(sampleId);
//如果有分析id 则是sample文件可以查询到对应关联文件内容
if (Objects.nonNull(analysisID)) {
//根据sampleId和分析id查询数据库文件信息
SpectrumFileRecord dbSpectrumFilePath = this.baseMapper.getDBSpectrumFilePath(sampleId, analysisID);
@ -204,7 +206,7 @@ public class GardsSampleDataWebServiceImpl extends ServiceImpl<GardsSampleDataWe
dir.mkdirs();
}
//获取sample文件路径 截取sample文件名称 将PHD改为zip
String zipFileName = dbSpectrumFilePath.getSampleFilePath().substring(dbSpectrumFilePath.getSampleFilePath().lastIndexOf(StringPool.SLASH) + 1).replace("PHD", "zip");
zipFileName = dbSpectrumFilePath.getSampleFilePath().substring(dbSpectrumFilePath.getSampleFilePath().lastIndexOf(StringPool.SLASH) + 1).replace("PHD", "zip");
//获取分析表中的采集开始时间
String collectStartStr = DateUtils.formatDate(dbSpectrumFilePath.getCollectStart(), "yyyy/MM/dd HH:mm:ss");
//根据探测器信息以及采集开始时间查询对应的qc文件路径
@ -238,31 +240,42 @@ public class GardsSampleDataWebServiceImpl extends ServiceImpl<GardsSampleDataWe
FileUtils.copyInputStreamToFile(qcStream, qcFile);
}
}
if (Objects.nonNull(sampleFile) || Objects.nonNull(gasFile) || Objects.nonNull(detFile) ||Objects.nonNull(qcFile)) {
//创建zip的临时文件
zipFile = File.createTempFile("betaGammaZip", null);
File zip = ZipUtil.zip(zipFile, true, sampleFile, gasFile, detFile, qcFile);
//获取压缩文件的文件输入流
inputStream = new FileInputStream(zip);
//重置响应信息
response.reset();
//设置响应类型
response.setContentType("application/download");
//解决中文不能生成文件
response.setHeader("Content-Disposition", "attachment; fileName=" + URLEncoder.encode(zipFileName,"UTF-8"));
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
//获取响应流的输出流
outputStream = response.getOutputStream();
//定义一个字节大小
byte[] buffer = new byte[1024];
int bytesRead;
// 将文件输出流写入到输出流中
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
} else {//如果没有分析id 则是其他谱类型文件 直接获取文件路径
GardsSampleDataWeb sampleData = getOneSample(sampleId);
String filePath = sampleData.getInputFileName();
if (StringUtils.isNotBlank(filePath)) {
zipFileName = filePath.substring(filePath.lastIndexOf(StringPool.SLASH)+1).replace("PHD", "zip");
sampleStream = ftpUtil.downloadFileStream(ftpUtil.getFtpRootPath() + spectrumPathProperties.getSaveFilePath()+ StringPool.SLASH + filePath);
if (Objects.nonNull(sampleStream)) {
sampleFile = new File(parameterProperties.getLogFilePath() + StringPool.SLASH + filePath.substring(filePath.lastIndexOf(StringPool.SLASH) + 1));
FileUtils.copyInputStreamToFile(sampleStream, sampleFile);
}
}
}
if (Objects.nonNull(sampleFile) || Objects.nonNull(gasFile) || Objects.nonNull(detFile) ||Objects.nonNull(qcFile)) {
//创建zip的临时文件
zipFile = File.createTempFile("betaGammaZip", null);
File zip = ZipUtil.zip(zipFile, true, sampleFile, gasFile, detFile, qcFile);
//获取压缩文件的文件输入流
inputStream = new FileInputStream(zip);
//重置响应信息
response.reset();
//设置响应类型
response.setContentType("application/download");
//解决中文不能生成文件
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Content-Disposition", "attachment; fileName=" + URLEncoder.encode(zipFileName,"UTF-8"));
//获取响应流的输出流
outputStream = response.getOutputStream();
//定义一个字节大小
byte[] buffer = new byte[1024];
int bytesRead;
// 将文件输出流写入到输出流中
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
@ -719,8 +732,7 @@ public class GardsSampleDataWebServiceImpl extends ServiceImpl<GardsSampleDataWe
public GardsSampleDataWeb getOneSample(Integer sampleId) {
LambdaQueryWrapper<GardsSampleDataWeb> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(GardsSampleDataWeb::getSampleId,sampleId);
return Optional.ofNullable(getOne(wrapper))
.orElse(new GardsSampleDataWeb());
return Optional.ofNullable(getOne(wrapper)).orElse(new GardsSampleDataWeb());
}
@Override