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 ddfa6443..75cb0fe3 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 @@ -27,7 +27,7 @@ public class LogManageController { @GetMapping("findFtpFolders") @ApiOperation(value = "查询日志文件夹树形结构", notes = "查询日志文件夹树形结构") - public List findFtpFolders(String workPath){ + public List findFtpFolders(String workPath) { return logManageService.findFtpFolders(workPath); } @@ -38,7 +38,7 @@ public class LogManageController { */ @GetMapping("findFiles") @ApiOperation(value = "查询目录下文件内容", notes = "查询目录下文件内容") - public List findFiles(String path){ + public List findFiles(String path) { return logManageService.findFiles(path); } 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 562aebea..bd824cd0 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,6 +1,7 @@ package org.jeecg.modules.service.impl; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; +import com.baomidou.mybatisplus.core.toolkit.StringPool; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.jeecg.common.util.DateUtils; @@ -23,14 +24,19 @@ public class LogManageServiceImpl implements ILogManageService { @Override public List findFtpFolders(String workPath) { List result = new ArrayList<>(); + FTPClient ftpClient = ftpUtil.LoginFTP(); + if(Objects.isNull(ftpClient)){ + throw new RuntimeException("ftp连接失败!"); + } try { - FTPClient ftpClient = ftpUtil.LoginFTP(); - if(Objects.isNull(ftpClient)){ - throw new RuntimeException("ftp连接失败!"); - } + //切换被动模式 + ftpClient.enterLocalPassiveMode(); + ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); + // 设置编码,当文件中存在中文且上传后文件乱码时可使用此配置项 + ftpClient.setControlEncoding("UTF-8"); + ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE); //切换工作文件路径 ftpClient.changeWorkingDirectory(workPath); - ftpClient.enterLocalPassiveMode(); List ftpFiles = Arrays.asList(ftpClient.listDirectories()); if (CollectionUtils.isNotEmpty(ftpFiles)){ int num =1; @@ -39,7 +45,7 @@ public class LogManageServiceImpl implements ILogManageService { logManage.setName(ftpFile.getName()); logManage.setOrderNum(num); logManage.setParentNum(0); - logManage.setPath(workPath + "/" + ftpFile.getName()); + logManage.setPath(workPath + StringPool.SLASH + ftpFile.getName()); result.add(logManage); num++; } @@ -47,16 +53,22 @@ public class LogManageServiceImpl implements ILogManageService { if (CollectionUtils.isNotEmpty(result)){ List list = new LinkedList<>(); for (LogManage logManage:result) { - list = this.findDirectory(ftpClient, list, logManage.getOrderNum(), workPath + "/" + logManage.getName()); + list = this.findDirectory(ftpClient, list, logManage.getOrderNum(), workPath + StringPool.SLASH + logManage.getName() , logManage.getName()); ftpClient.changeToParentDirectory(); } result.addAll(list); } - if (ftpClient != null){ - ftpClient.disconnect(); - } + } 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; @@ -65,73 +77,75 @@ public class LogManageServiceImpl implements ILogManageService { @Override public List findFiles(String path) { List result = new ArrayList<>(); + FTPClient ftpClient = ftpUtil.LoginFTP(); + if (Objects.isNull(ftpClient)){ + throw new RuntimeException("ftp连接失败!"); + } try { - FTPClient ftpClient = ftpUtil.LoginFTP(); - if (Objects.isNull(ftpClient)){ - throw new RuntimeException("ftp连接失败!"); - } - List paths = Arrays.asList(path.split("/")); - if (CollectionUtils.isNotEmpty(paths)){ - for (String workPath:paths) { - //切换工作文件路径 - ftpClient.changeWorkingDirectory(workPath); - } - } + //切换被动模式 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 +"/"+ 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 (ftpClient != null){ - ftpClient.disconnect(); - } } catch (IOException e) { throw new RuntimeException(e); + } finally { + try { + if (ftpClient != null){ + ftpClient.disconnect(); + } + } catch (IOException e) { + throw new RuntimeException(e); + } } return result; } /** * 遍历查询当前路径下的文件夹信息 - * @param ftp + * @param ftpClient * @param list * @param filePath 以"/"开始和结束 * @return */ - public List findDirectory(FTPClient ftp,List list,Integer parentNum,String filePath){ + public List findDirectory(FTPClient ftpClient, List list, Integer parentNum, String filePath, String fileName){ try { - if (filePath.indexOf("/")>0){ - List paths = Arrays.asList(filePath.split("/")); - for (String path:paths) { - ftp.changeWorkingDirectory(path); - } - } - List ftpFiles = Arrays.asList(ftp.listDirectories()); + //切换被动模式 + ftpClient.enterLocalPassiveMode(); + ftpClient.changeWorkingDirectory(fileName); + List ftpFiles = Arrays.asList(ftpClient.listDirectories()); if (CollectionUtils.isNotEmpty(ftpFiles)){ - int num =1; + int num = 1; for (FTPFile file : ftpFiles) { if (file.isDirectory()) { LogManage logManage = new LogManage(); logManage.setName(file.getName()); - logManage.setOrderNum(num); + logManage.setOrderNum(parentNum*10+num); logManage.setParentNum(parentNum); - logManage.setPath(filePath +"/"+ file.getName()); + logManage.setPath(filePath + StringPool.SLASH + file.getName()); list.add(logManage); - num++; // 需要加此判断。否则,ftp默认将‘项目文件所在目录之下的目录(./)’与‘项目文件所在目录向上一级目录下的目录(../)’都纳入递归,这样下去就陷入一个死循环了。需将其过滤掉。 if (!".".equals(file.getName()) && !"..".equals(file.getName())) { - findDirectory(ftp,list,num,filePath +"/"+ file.getName()); - ftp.changeToParentDirectory(); + findDirectory(ftpClient, list, parentNum*10+num, filePath + StringPool.SLASH + file.getName(), file.getName()); + ftpClient.changeToParentDirectory(); } + num++; } } }