diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/ExportUtil.java b/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/ExportUtil.java index f887987f..3640f577 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/ExportUtil.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/ExportUtil.java @@ -1,7 +1,9 @@ package org.jeecg.common.util; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; +import org.apache.commons.io.FileUtils; import org.apache.poi.ss.usermodel.Workbook; import org.jeecgframework.poi.excel.ExcelExportUtil; import org.jeecgframework.poi.excel.entity.ExportParams; @@ -15,6 +17,8 @@ import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -102,9 +106,23 @@ public class ExportUtil { String pathPrefix = "excelTemplate/"; String path = pathPrefix + template; InputStream inputStream = classPathStream(path); - - String templatePath = ""; - return new TemplateExportParams(templatePath); + String tempDir = System.getProperty("java.io.tmpdir"); + String tempPath = tempDir + File.separator + template; + try { + File tempFile = File.createTempFile(tempPath, null); + FileUtils.copyInputStreamToFile(inputStream, tempFile); + String templatePath = tempFile.getAbsolutePath(); + return new TemplateExportParams(templatePath); + } catch (IOException e) { + e.printStackTrace(); + return null; + }finally { + try { + if (ObjectUtil.isNotNull(inputStream)) inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } public static void exportXls(HttpServletResponse response, String template, diff --git a/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/service/impl/SpectrumAnalysisServiceImpl.java b/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/service/impl/SpectrumAnalysisServiceImpl.java index 0c53010f..771e32cf 100644 --- a/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/service/impl/SpectrumAnalysisServiceImpl.java +++ b/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/service/impl/SpectrumAnalysisServiceImpl.java @@ -15,6 +15,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.google.common.cache.Cache; import com.google.common.collect.Lists; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.shiro.SecurityUtils; @@ -4398,45 +4399,49 @@ public class SpectrumAnalysisServiceImpl implements ISpectrumAnalysisService { analyze.put("gasRoi", roiChannels(roiChannelsDtosG)); analyze.putAll(roiResults(roiResultsDtos)); analyze.put("resultSummary", xeResults(xeResultsDtos)); - // 使数据适配导出模板 - String pathPrefix = "excelTemplate/"; - String path = pathPrefix + SAVETOTXT.getName(); - String template = ClassUtil.classPath(path); - List lines = FileUtil.readUtf8Lines(template); - // 正则表达式,匹配${}中的内容 - String regex = "\\$\\{([^}]+)}"; - List newLines = new ArrayList<>(); - List list = ListUtil.toList("sampleNewCalibration","sampleRoi", "detNewCalibration", "detRoi", "gasNewCalibration", "gasRoi","grossRoi", - "netRoi", "concRoi", "resultSummary"); - List skip = ListUtil.toList("${sampleNewCalibration}","${sampleRoi}", "${detNewCalibration}", "${detRoi}", "${gasNewCalibration}", "${gasRoi}", - "${grossRoi}", "${netRoi}", "${concRoi}", "${resultSummary}"); - for (String line : lines) { - List fieldNames = ReUtil.findAllGroup1(regex, line); - if (CollUtil.isEmpty(fieldNames)){ - newLines.add(line); - continue; - } - for (String fieldName : fieldNames) { - Object value = analyze.get(fieldName); - if (CollUtil.contains(list, fieldName)){ - newLines.addAll((List)value); - }else { - String search = "${" + fieldName + "}"; - String replacement = StrUtil.toString(value); - replacement = StrUtil.isBlank(replacement) ? "null" : replacement; - line = StrUtil.replace(line, search, replacement); - } - } - if (!CollUtil.contains(skip, line)) - newLines.add(line); - } + PrintWriter writer = null; try { - String export = "SaveToTxt.txt"; - writer = ExportUtil.streamWriter(response, export); - for (String newLine : newLines) { - writer.println(newLine); + // 使数据适配导出模板 + String pathPrefix = "excelTemplate/"; + String path = pathPrefix + SAVETOTXT.getName(); + /*String template = ClassUtil.classPath(path); + List lines = FileUtil.readUtf8Lines(template);*/ + InputStream inputStream = ClassUtil.classPathStream(path); + List lines = IOUtils.readLines(inputStream, "UTF-8"); + // 正则表达式,匹配${}中的内容 + String regex = "\\$\\{([^}]+)}"; + List newLines = new ArrayList<>(); + List list = ListUtil.toList("sampleNewCalibration","sampleRoi", "detNewCalibration", "detRoi", "gasNewCalibration", "gasRoi","grossRoi", + "netRoi", "concRoi", "resultSummary"); + List skip = ListUtil.toList("${sampleNewCalibration}","${sampleRoi}", "${detNewCalibration}", "${detRoi}", "${gasNewCalibration}", "${gasRoi}", + "${grossRoi}", "${netRoi}", "${concRoi}", "${resultSummary}"); + for (String line : lines) { + List fieldNames = ReUtil.findAllGroup1(regex, line); + if (CollUtil.isEmpty(fieldNames)){ + newLines.add(line); + continue; + } + for (String fieldName : fieldNames) { + Object value = analyze.get(fieldName); + if (CollUtil.contains(list, fieldName)){ + newLines.addAll((List)value); + }else { + String search = "${" + fieldName + "}"; + String replacement = StrUtil.toString(value); + replacement = StrUtil.isBlank(replacement) ? "null" : replacement; + line = StrUtil.replace(line, search, replacement); + } + } + if (!CollUtil.contains(skip, line)) + newLines.add(line); } + + String export = "SaveToTxt.txt"; + writer = ExportUtil.streamWriter(response, export); + for (String newLine : newLines) { + writer.println(newLine); + } } catch (IOException e) { e.printStackTrace(); }finally {