logMange树形结构问题修改
This commit is contained in:
parent
a210b75205
commit
d1dcb4b5cc
|
@ -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<LogManage> findFtpFolders(String workPath) {
|
||||
List<LogManage> result = new ArrayList<>();
|
||||
try {
|
||||
FTPClient ftpClient = ftpUtil.LoginFTP();
|
||||
if(Objects.isNull(ftpClient)){
|
||||
throw new RuntimeException("ftp连接失败!");
|
||||
}
|
||||
try {
|
||||
//切换被动模式
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||||
// 设置编码,当文件中存在中文且上传后文件乱码时可使用此配置项
|
||||
ftpClient.setControlEncoding("UTF-8");
|
||||
ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
|
||||
//切换工作文件路径
|
||||
ftpClient.changeWorkingDirectory(workPath);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
List<FTPFile> 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,17 +53,23 @@ public class LogManageServiceImpl implements ILogManageService {
|
|||
if (CollectionUtils.isNotEmpty(result)){
|
||||
List<LogManage> 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);
|
||||
}
|
||||
|
||||
} 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<FileInfo> findFiles(String path) {
|
||||
List<FileInfo> result = new ArrayList<>();
|
||||
try {
|
||||
FTPClient ftpClient = ftpUtil.LoginFTP();
|
||||
if (Objects.isNull(ftpClient)){
|
||||
throw new RuntimeException("ftp连接失败!");
|
||||
}
|
||||
List<String> paths = Arrays.asList(path.split("/"));
|
||||
if (CollectionUtils.isNotEmpty(paths)){
|
||||
for (String workPath:paths) {
|
||||
//切换工作文件路径
|
||||
ftpClient.changeWorkingDirectory(workPath);
|
||||
}
|
||||
}
|
||||
try {
|
||||
//切换被动模式
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||||
// 设置编码,当文件中存在中文且上传后文件乱码时可使用此配置项
|
||||
ftpClient.setControlEncoding("UTF-8");
|
||||
ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
|
||||
//切换工作文件路径
|
||||
ftpClient.changeWorkingDirectory(path);
|
||||
List<FTPFile> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
} 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<LogManage> findDirectory(FTPClient ftp,List<LogManage> list,Integer parentNum,String filePath){
|
||||
public List<LogManage> findDirectory(FTPClient ftpClient, List<LogManage> list, Integer parentNum, String filePath, String fileName){
|
||||
try {
|
||||
if (filePath.indexOf("/")>0){
|
||||
List<String> paths = Arrays.asList(filePath.split("/"));
|
||||
for (String path:paths) {
|
||||
ftp.changeWorkingDirectory(path);
|
||||
}
|
||||
}
|
||||
List<FTPFile> ftpFiles = Arrays.asList(ftp.listDirectories());
|
||||
//切换被动模式
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.changeWorkingDirectory(fileName);
|
||||
List<FTPFile> 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(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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user