From 89075bef6d4c935760ceb44f8473b97bf0cd61cb Mon Sep 17 00:00:00 2001 From: nieziyan Date: Thu, 21 Mar 2024 18:08:11 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A1.=E6=97=A5=E5=BF=97=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E6=A8=A1=E5=9D=97FTP=E6=9C=8D=E5=8A=A1=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E4=B8=BA=E6=9C=AC=E5=9C=B0=E6=96=87=E4=BB=B6=E6=9C=8D?= =?UTF-8?q?=E5=8A=A12.=E6=97=A5=E5=BF=97=E6=96=87=E4=BB=B6=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=A2=9E=E5=8A=A0=E5=88=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/comparator/FileComparator.java | 4 +- .../controller/LogManageController.java | 19 +- .../modules/service/ILogManageService.java | 12 +- .../service/impl/LogManageServiceImpl.java | 263 ++++++++---------- .../src/main/resources/application.yml | 3 +- 5 files changed, 132 insertions(+), 169 deletions(-) diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/modules/base/comparator/FileComparator.java b/jeecg-boot-base-core/src/main/java/org/jeecg/modules/base/comparator/FileComparator.java index 28626188..4250675b 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/modules/base/comparator/FileComparator.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/modules/base/comparator/FileComparator.java @@ -6,8 +6,8 @@ import java.util.Comparator; public class FileComparator implements Comparator { - private String field; - private String order; + private final String field; + private final String order; public FileComparator(String field, String order) { this.field = field; 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 bf783238..9b2ff809 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 @@ -2,7 +2,9 @@ package org.jeecg.modules.controller; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.jeecg.common.api.vo.Result; import org.jeecg.common.util.FTPUtil; +import org.jeecg.modules.base.bizVo.FileVo; import org.jeecg.modules.entity.FileInfo; import org.jeecg.modules.entity.LogManage; import org.jeecg.modules.service.ILogManageService; @@ -20,31 +22,24 @@ import java.util.List; @Api(value = "日志管理", tags = "日志管理") public class LogManageController { - @Autowired - private FTPUtil ftpUtil; @Autowired private ILogManageService logManageService; @GetMapping("findFtpFolders") @ApiOperation(value = "查询日志文件夹树形结构", notes = "查询日志文件夹树形结构") public List findFtpFolders(String workPath) { - return logManageService.findFtpFolders(workPath); + return logManageService.fileTree(workPath); } - /** - * 查询目录下文件内容 - * @param path - * @return - */ @GetMapping("findFiles") @ApiOperation(value = "查询目录下文件内容", notes = "查询目录下文件内容") - public List findFiles(String path) { - return logManageService.findFiles(path); + public Result findFiles(String path, FileVo fileVo) { + return logManageService.findFiles(path, fileVo); } @PostMapping("downloadFile") @ApiOperation(value = "ftp文件下载", notes = "ftp文件下载") - public void downloadFile(String localPath, String fileName, HttpServletResponse response) { - ftpUtil.downloadFTPFile(localPath, response); + public void downloadFile(String localPath, HttpServletResponse response) { + logManageService.downloadFile(localPath, response); } } diff --git a/jeecg-module-log-manage/src/main/java/org/jeecg/modules/service/ILogManageService.java b/jeecg-module-log-manage/src/main/java/org/jeecg/modules/service/ILogManageService.java index bab2dce0..5a073fc1 100644 --- a/jeecg-module-log-manage/src/main/java/org/jeecg/modules/service/ILogManageService.java +++ b/jeecg-module-log-manage/src/main/java/org/jeecg/modules/service/ILogManageService.java @@ -1,24 +1,24 @@ package org.jeecg.modules.service; +import org.jeecg.common.api.vo.Result; +import org.jeecg.modules.base.bizVo.FileVo; import org.jeecg.modules.entity.FileInfo; import org.jeecg.modules.entity.LogManage; +import javax.servlet.http.HttpServletResponse; import java.util.List; public interface ILogManageService { /** * 查询日志文件夹树形结构 - * @param workPath - * @return */ - List findFtpFolders(String workPath); + List fileTree(String workPath); /** * 查询目录下文件内容 - * @param path - * @return */ - List findFiles(String path); + Result findFiles(String path, FileVo fileVo); + void downloadFile(String localPath, HttpServletResponse response); } diff --git a/jeecg-module-log-manage/src/main/java/org/jeecg/modules/service/impl/LogManageServiceImpl.java b/jeecg-module-log-manage/src/main/java/org/jeecg/modules/service/impl/LogManageServiceImpl.java index 3138b956..33fc1c18 100644 --- a/jeecg-module-log-manage/src/main/java/org/jeecg/modules/service/impl/LogManageServiceImpl.java +++ b/jeecg-module-log-manage/src/main/java/org/jeecg/modules/service/impl/LogManageServiceImpl.java @@ -1,192 +1,159 @@ package org.jeecg.modules.service.impl; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.collection.ListUtil; +import cn.hutool.core.date.DateTime; +import cn.hutool.core.date.DateUtil; +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 com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; +import org.jeecg.common.api.vo.Result; +import org.jeecg.common.constant.Prompt; +import org.jeecg.common.properties.SpectrumPathProperties; import org.jeecg.common.util.DateUtils; -import org.jeecg.common.util.FTPUtil; +import org.jeecg.common.util.ExportUtil; +import org.jeecg.common.util.PageUtil; +import org.jeecg.modules.base.bizVo.FileVo; import org.jeecg.modules.entity.FileInfo; import org.jeecg.modules.entity.LogManage; import org.jeecg.modules.service.ILogManageService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import javax.servlet.http.HttpServletResponse; +import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.*; +import java.util.stream.Collectors; -@Service("logManageService") +@Slf4j +@Service public class LogManageServiceImpl implements ILogManageService { @Autowired - private FTPUtil ftpUtil; + private SpectrumPathProperties spectrumPath; @Override - public List findFtpFolders(String workPath) { + public List fileTree(String workPath) { List result = new ArrayList<>(); - FTPClient ftpClient = ftpUtil.LoginFTP(); - if(Objects.isNull(ftpClient)){ - throw new RuntimeException("ftp connection failed!"); + workPath = spectrumPath.getRootPath() + StringPool.SLASH + workPath; + List files = ListUtil.toList(FileUtil.ls(workPath)); + if (CollUtil.isEmpty(files)) return result; + int num = 1; + for (File file : files) { + LogManage logManage = new LogManage(); + logManage.setName(file.getName()); + logManage.setOrderNum(num++); + logManage.setPath(workPath + StringPool.SLASH + file.getName()); + List children = this.getChildren(logManage); + logManage.setHashChild(CollUtil.isNotEmpty(children)); + logManage.setChildren(children); + result.add(logManage); } - try { - //切换被动模式 - ftpClient.enterLocalPassiveMode(); - ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); - // 设置编码,当文件中存在中文且上传后文件乱码时可使用此配置项 - ftpClient.setControlEncoding("UTF-8"); - ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE); - //切换工作文件路径 - workPath = ftpUtil.getFtpRootPath()+StringPool.SLASH+workPath; - ftpClient.changeWorkingDirectory(workPath); - List ftpFiles = Arrays.asList(ftpClient.listDirectories()); - if (CollectionUtils.isNotEmpty(ftpFiles)){ - int num =1; - for (FTPFile ftpFile:ftpFiles) { - LogManage logManage = new LogManage(); - logManage.setName(ftpFile.getName()); - logManage.setOrderNum(num); - logManage.setParentNum(0); - logManage.setPath(workPath + StringPool.SLASH + ftpFile.getName()); - result.add(logManage); - num++; - } - } - if (CollectionUtils.isNotEmpty(result)){ - List list = new LinkedList<>(); - for (LogManage logManage:result) { - list = this.findDirectory(ftpClient, list, logManage.getOrderNum(), workPath + StringPool.SLASH + logManage.getName() , logManage.getName()); - ftpClient.changeToParentDirectory(); - } - result.addAll(list); - } - - } catch (IOException e) { - throw new RuntimeException(e); - } finally { - try { - if (ftpClient != null){ - ftpClient.disconnect(); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } - result = this.LogManageTree(result); return result; } @Override - public List findFiles(String path) { - List result = new ArrayList<>(); - FTPClient ftpClient = ftpUtil.LoginFTP(); - if (Objects.isNull(ftpClient)){ - throw new RuntimeException("ftp connection failed!"); + public Result findFiles(String path, FileVo fileVo) { + String name = fileVo.getName(); + Integer pageNo = fileVo.getPageNo(); + Integer pageSize = fileVo.getPageSize(); + Page page = new Page<>(pageNo, pageSize); + List files = CollUtil.toList(FileUtil.ls(path)); + // 文件名过滤 + if (StrUtil.isNotBlank(name)) + files = files.stream().filter(file -> StrUtil.containsIgnoreCase(file.getName(), name)) + .collect(Collectors.toList()); + page.setTotal(files.size()); + // 分页 + files = PageUtil.page(pageNo, pageSize, files); + List records = new ArrayList<>(); + for (File file : files) { + if (FileUtil.isDirectory(file)) continue; + FileInfo fileInfo = new FileInfo(); + fileInfo.setFileName(file.getName()); + fileInfo.setFilePath(path + StringPool.SLASH + file.getName()); + fileInfo.setFileSize(FileUtil.readableFileSize(file)); + fileInfo.setFileDate(DateUtil.formatDateTime(FileUtil.lastModifiedTime(file))); + records.add(fileInfo); } + page.setRecords(records); + return Result.OK(page); + } + + @Override + public void downloadFile(String localPath, HttpServletResponse response) { + OutputStream outputStream = null; + InputStream inputStream = null; try { - //切换被动模式 - ftpClient.enterLocalPassiveMode(); - ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); - // 设置编码,当文件中存在中文且上传后文件乱码时可使用此配置项 - ftpClient.setControlEncoding("UTF-8"); - ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE); - //切换工作文件路径 - ftpClient.changeWorkingDirectory(path); - List ftpFiles = Arrays.asList(ftpClient.listFiles()); - if (CollectionUtils.isNotEmpty(ftpFiles)){ - for (FTPFile ftpFile:ftpFiles) { - if (ftpFile.isFile()){ - FileInfo fileInfo = new FileInfo(); - fileInfo.setFileName(ftpFile.getName()); - fileInfo.setFilePath(path + StringPool.SLASH + ftpFile.getName()); - fileInfo.setFileSize(String.format("%.2f", Double.valueOf(Double.valueOf(ftpFile.getSize())/1024)) + "KB"); - fileInfo.setFileDate(DateUtils.formatDate(ftpFile.getTimestamp(),"yyyy-MM-dd")); - result.add(fileInfo); - } - } + // 如果是目录 则直接退出方法 + if (FileUtil.isDirectory(localPath)) return; + + // 判断是否存在此文件 + if (!FileUtil.exist(localPath)) return; + + // 获取文件名 + String fileName = FileUtil.getName(localPath); + + inputStream = FileUtil.getInputStream(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) { - throw new RuntimeException(e); + log.error("文件{}下载失败 :{}", localPath, e.getMessage()); } finally { try { - if (ftpClient != null){ - ftpClient.disconnect(); - } + if (ObjectUtil.isNotNull(inputStream))inputStream.close(); + if (ObjectUtil.isNotNull(outputStream))outputStream.close(); } catch (IOException e) { - throw new RuntimeException(e); + e.printStackTrace(); } } - return result; } /** - * 遍历查询当前路径下的文件夹信息 - * @param ftpClient - * @param list - * @param filePath 以"/"开始和结束 - * @return + * 获取当前目录节点所有子孙节点Tree */ - public List findDirectory(FTPClient ftpClient, List list, Integer parentNum, String filePath, String fileName){ - try { - //切换被动模式 - ftpClient.enterLocalPassiveMode(); - ftpClient.changeWorkingDirectory(fileName); - List ftpFiles = Arrays.asList(ftpClient.listDirectories()); - if (CollectionUtils.isNotEmpty(ftpFiles)){ - int num = 1; - for (FTPFile file : ftpFiles) { - if (file.isDirectory()) { - LogManage logManage = new LogManage(); - logManage.setName(file.getName()); - logManage.setOrderNum(parentNum*10+num); - logManage.setParentNum(parentNum); - logManage.setPath(filePath + StringPool.SLASH + file.getName()); - list.add(logManage); - // 需要加此判断。否则,ftp默认将‘项目文件所在目录之下的目录(./)’与‘项目文件所在目录向上一级目录下的目录(../)’都纳入递归,这样下去就陷入一个死循环了。需将其过滤掉。 - if (!".".equals(file.getName()) && !"..".equals(file.getName())) { - findDirectory(ftpClient, list, parentNum*10+num, filePath + StringPool.SLASH + file.getName(), file.getName()); - ftpClient.changeToParentDirectory(); - } - num++; - } - } - } - } catch (IOException e) { - throw new RuntimeException(e); - } - return list; - } - - /** - * 将当前的文件夹转换成树形结构 - * @param logManages - * @return - */ - public List LogManageTree(List logManages){ - if (logManages == null) { - return null; - } + private List getChildren(LogManage parent){ List result = new LinkedList<>(); - Integer TOP_NODE_ID = 0; - logManages.forEach(logManage -> { - Integer pid = logManage.getParentNum(); - if (pid == null || TOP_NODE_ID.equals(pid)) { - result.add(logManage); - return; - } - for (LogManage manage : logManages) { - Integer id = manage.getOrderNum(); - if (id != null && id.equals(pid)) { - if (manage.getChildren() == null) { - manage.initChildren(); - } - logManage.setHashParent(true); - manage.getChildren().add(logManage); - manage.setHashChild(true); - return; - } - } - }); + String parentPath = parent.getPath(); + // 如果是文件 则直接返回空集合 + if (FileUtil.isFile(parentPath)) return result; + List files = ListUtil.toList(FileUtil.ls(parentPath)); + // 如果当前目录不存在子文件 则返回空集合 + if (CollUtil.isEmpty(files)) return result; + int parentOrderNum = parent.getOrderNum(); + int num = parentOrderNum * 10 + 1; + for (File file : files) { + // 过滤掉文件 只收集目录 + if (FileUtil.isFile(file)) continue; + LogManage logManage = new LogManage(); + logManage.setName(file.getName()); + logManage.setOrderNum(num++); + logManage.setHashParent(true); + logManage.setParentNum(parentOrderNum); + logManage.setPath(parentPath + StringPool.SLASH + file.getName()); + List children = getChildren(logManage); + logManage.setHashChild(CollUtil.isNotEmpty(children)); + logManage.setChildren(children); + result.add(logManage); + } return result; } - } diff --git a/jeecg-server-cloud/armd-log-manage-start/src/main/resources/application.yml b/jeecg-server-cloud/armd-log-manage-start/src/main/resources/application.yml index a6d71a86..4ba3ba15 100644 --- a/jeecg-server-cloud/armd-log-manage-start/src/main/resources/application.yml +++ b/jeecg-server-cloud/armd-log-manage-start/src/main/resources/application.yml @@ -15,4 +15,5 @@ spring: config: import: - optional:nacos:armd.yaml - - optional:nacos:armd-@profile.name@.yaml \ No newline at end of file + - optional:nacos:armd-@profile.name@.yaml + - optional:nacos:armd-analysis-@profile.name@.yaml \ No newline at end of file