feat:log manage查询方式修改

This commit is contained in:
qiaoqinzheng 2023-05-23 08:49:28 +08:00
parent a74d96bc51
commit b658e37e19
2 changed files with 39 additions and 28 deletions

View File

@ -1,6 +1,7 @@
package org.jeecg.common.util;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
@ -15,6 +16,7 @@ import java.util.Arrays;
import java.util.List;
@Component
@Slf4j
public class FTPUtil {
@Value("${ftp.host}")
@ -34,10 +36,9 @@ public class FTPUtil {
/**
* 登录ftp
* @param workPath
* @return
*/
public FTPClient LoginFTP(String workPath){
public FTPClient LoginFTP(){
//声明FTP客户端
FTPClient ftp = new FTPClient();
try {
@ -51,13 +52,6 @@ public class FTPUtil {
ftp.disconnect();
return null;
}
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//切换工作文件路径
ftp.changeWorkingDirectory(workPath);
// 设置编码当文件中存在中文且上传后文件乱码时可使用此配置项
ftp.setControlEncoding(encoding);
// 切换为本地被动模式可以解决FTP上传后文件为空的问题但需要服务器将FTP服务添加至防火墙白名单
ftp.enterLocalPassiveMode();
} catch (IOException e) {
throw new RuntimeException(e);
}
@ -83,12 +77,12 @@ public class FTPUtil {
logManage.setName(file.getName());
logManage.setOrderNum(num);
logManage.setParentNum(parentNum);
logManage.setPath(filePath+ file.getName() + "/");
logManage.setPath(filePath+ file.getName());
list.add(logManage);
num++;
// 需要加此判断否则ftp默认将项目文件所在目录之下的目录./项目文件所在目录向上一级目录下的目录../都纳入递归这样下去就陷入一个死循环了需将其过滤掉
if (!".".equals(file.getName()) && !"..".equals(file.getName())) {
findDirectory(ftp,list,num,filePath + file.getName() + "/");
findDirectory(ftp,list,num,file.getName());
}
}
}
@ -101,9 +95,21 @@ public class FTPUtil {
public InputStream downloadFTPFile(String localPath, String fileName){
InputStream in = null;
FTPClient ftpClient = this.LoginFTP(localPath);
FTPClient ftpClient = this.LoginFTP();
//传输模式
try {
List<String> paths = Arrays.asList(localPath.split("/"));
if (CollectionUtils.isNotEmpty(paths)){
for (String workPath:paths) {
//切换工作文件路径
ftpClient.changeWorkingDirectory(workPath);
}
}
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 设置编码当文件中存在中文且上传后文件乱码时可使用此配置项
ftpClient.setControlEncoding(encoding);
// 切换为本地被动模式可以解决FTP上传后文件为空的问题但需要服务器将FTP服务添加至防火墙白名单
// ftp.enterLocalPassiveMode();
ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
List<FTPFile> ftpFiles = Arrays.asList(ftpClient.listFiles());
if (CollectionUtils.isNotEmpty(ftpFiles)){

View File

@ -33,12 +33,13 @@ public class LogManageController {
@ApiOperation(value = "查询日志文件夹树形结构", notes = "查询日志文件夹树形结构")
public List<LogManage> findFtpFolders(String workPath){
List<LogManage> result = new ArrayList<>();
workPath = "/"+workPath;
FTPClient ftpClient = ftpUtil.LoginFTP(workPath);
if(Objects.isNull(ftpClient)){
throw new RuntimeException("ftp连接失败!");
}
try {
FTPClient ftpClient = ftpUtil.LoginFTP();
//切换工作文件路径
ftpClient.changeWorkingDirectory(workPath);
if(Objects.isNull(ftpClient)){
throw new RuntimeException("ftp连接失败!");
}
List<FTPFile> ftpFiles = Arrays.asList(ftpClient.listDirectories());
if (CollectionUtils.isNotEmpty(ftpFiles)){
int num =1;
@ -47,7 +48,7 @@ public class LogManageController {
logManage.setName(ftpFile.getName());
logManage.setOrderNum(num);
logManage.setParentNum(0);
logManage.setPath(workPath+ "/" + ftpFile.getName() + "/");
logManage.setPath(workPath+ "/" + ftpFile.getName());
result.add(logManage);
num++;
}
@ -55,7 +56,7 @@ public class LogManageController {
if (CollectionUtils.isNotEmpty(result)){
List<LogManage> list = new LinkedList<>();
for (LogManage logManage:result) {
list = ftpUtil.findDirectory(ftpClient, list, logManage.getOrderNum(), workPath + "/" + logManage.getName() + "/");
list = ftpUtil.findDirectory(ftpClient, list, logManage.getOrderNum(), logManage.getName());
}
result.addAll(list);
}
@ -108,21 +109,25 @@ public class LogManageController {
@ApiOperation(value = "查询目录下文件内容", notes = "查询目录下文件内容")
public List<FileInfo> findFiles(String path){
List<FileInfo> result = new ArrayList<>();
if (!path.endsWith("/")){
path = path+"/";
}
FTPClient ftpClient = ftpUtil.LoginFTP(path);
if (Objects.isNull(ftpClient)){
throw new RuntimeException("ftp连接失败!");
}
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);
}
}
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+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);
@ -139,7 +144,7 @@ public class LogManageController {
@ApiOperation(value = "ftp文件下载", notes = "ftp文件下载")
public void downloadFile(String localPath, String fileName, HttpServletResponse response) throws IOException {
if (localPath.contains(fileName)){
localPath=localPath.substring(0,localPath.indexOf(fileName));
localPath=localPath.substring(0,localPath.indexOf(fileName)-1);
}
//重置响应信息
response.reset();