From eb35a181f6b5e2ce9e49ffec91593cfc77533124 Mon Sep 17 00:00:00 2001 From: nieziyan Date: Wed, 8 Nov 2023 16:33:40 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9AFTPDownLoadFile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/jeecg/common/util/FTPUtil.java | 57 +++++++++++++++++++ .../controller/LogManageController.java | 6 +- 2 files changed, 58 insertions(+), 5 deletions(-) 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); } - }