diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/FTPUtil.java b/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/FTPUtil.java index 497bf8a5..4ce37ade 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/FTPUtil.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/FTPUtil.java @@ -1,9 +1,12 @@ package org.jeecg.common.util; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; +import com.baomidou.mybatisplus.core.toolkit.StringPool; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; @@ -11,6 +14,8 @@ import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; +import org.jeecg.common.api.vo.Result; +import org.jeecg.common.constant.Prompt; import org.jeecg.common.constant.SymbolConstant; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -195,6 +200,58 @@ public class FTPUtil { } } + public void downloadFTPFile(String localPath, HttpServletResponse response) { + // 连接FTP + FTPClient ftpClient = this.LoginFTP(); + // 判断FTP是否连接成功 + if (Objects.isNull(ftpClient)){ + throw new RuntimeException(Prompt.FTP_ERR); + } + + OutputStream outputStream = null; + InputStream inputStream = null; + try { + // 判断FTP服务器上是否存在此文件 + String[] files = ftpClient.listNames(localPath); + if (ArrayUtil.isEmpty(files)) return; + + // 存在多个文件名表示此路径为目录而非文件 + if (ArrayUtil.length(files) > 1) return; + + // 获取文件名 + String fileName = FileUtil.getName(localPath); + + // 在当前工作路径下读取文件 + ftpClient.enterLocalPassiveMode(); + ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); + // 设置编码,当文件中存在中文且上传后文件乱码时可使用此配置项 + ftpClient.setControlEncoding(encoding); + ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE); + inputStream = ftpClient.retrieveFileStream(localPath); + outputStream = ExportUtil.stream(response, fileName); + + // 缓冲区大小 + byte[] buffer = new byte[4096]; + int bytesRead; + + // 将文件输出流写入到输出流中 + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + } + catch (IOException e) { + e.printStackTrace(); + }finally { + try { + if (ObjectUtil.isNotNull(inputStream))inputStream.close(); + if (ObjectUtil.isNotNull(outputStream))outputStream.close(); + ftpClient.disconnect(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + /** * 检查目录是否存在,不存在则创建,支持递归创建 * @param path 目录路径 diff --git a/jeecg-module-log-manage/src/main/java/org/jeecg/modules/controller/LogManageController.java b/jeecg-module-log-manage/src/main/java/org/jeecg/modules/controller/LogManageController.java index 75cb0fe3..bf783238 100644 --- a/jeecg-module-log-manage/src/main/java/org/jeecg/modules/controller/LogManageController.java +++ b/jeecg-module-log-manage/src/main/java/org/jeecg/modules/controller/LogManageController.java @@ -45,10 +45,6 @@ public class LogManageController { @PostMapping("downloadFile") @ApiOperation(value = "ftp文件下载", notes = "ftp文件下载") public void downloadFile(String localPath, String fileName, HttpServletResponse response) { - if (localPath.contains(fileName)){ - localPath=localPath.substring(0,localPath.indexOf(fileName)-1); - } - ftpUtil.downloadFTPFile(localPath, fileName,response); + ftpUtil.downloadFTPFile(localPath, response); } - } 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 4761f5b1..0f87d7b6 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 @@ -1255,17 +1255,17 @@ public class SpectrumAnalysisServiceImpl extends AbstractLogOrReport implements } @Override - public void exportSampleInformation(Integer sampleId, String sampleFileName, HttpServletRequest request, HttpServletResponse response) { - Result result = viewSampleInformation(sampleId, sampleFileName, request); - StringBuffer buffer = result.getResult(); - if (ObjectUtil.isNull(buffer)) return; - String sampleInfo = buffer.toString(); + public void exportSampleInformation(Integer sampleId, String sampleFileName, + HttpServletRequest request, HttpServletResponse response) { + Result result = viewSampleInformation(sampleId, sampleFileName, request); + String sampleInfo = result.getResult(); + if (StrUtil.isBlank(sampleInfo)) return; String colon = SymbolConstant.COLON; String space = SymbolConstant.SPACE; String line = SymbolConstant.LINE; List sampleInfos = ListUtil.toList(sampleInfo.split(line)).stream() .filter(StrUtil::isNotBlank) - .filter(item -> StrUtil.contains(item,colon)) + .filter(item -> StrUtil.contains(item, colon)) .collect(Collectors.toList()); Map dataMap = new HashMap<>(); for (String s : sampleInfos) { @@ -3777,7 +3777,7 @@ public class SpectrumAnalysisServiceImpl extends AbstractLogOrReport implements export = StrUtil.replace(sampleFileName, ".PHD", ".xls"); } String template = SAVETOEXCEL.getName(); - ExportUtil.exportXls(response, template, analyze,export); + ExportUtil.exportXls(response, template, analyze, export); } @Override