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