ftp工具类修改位置
This commit is contained in:
parent
1095f9345b
commit
b6c3f270f8
|
@ -252,6 +252,12 @@
|
|||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
</dependency>
|
||||
<!--ftp依赖-->
|
||||
<dependency>
|
||||
<groupId>commons-net</groupId>
|
||||
<artifactId>commons-net</artifactId>
|
||||
<version>3.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -5,7 +5,6 @@ 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;
|
||||
import org.jeecg.modules.entity.LogManage;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
@ -61,47 +60,6 @@ public class FTPUtil {
|
|||
return ftp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 遍历查询当前路径下的文件夹信息
|
||||
* @param ftp
|
||||
* @param list
|
||||
* @param filePath 以"/"开始和结束
|
||||
* @return
|
||||
*/
|
||||
public List<LogManage> findDirectory(FTPClient ftp,List<LogManage> list,Integer parentNum,String filePath){
|
||||
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());
|
||||
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.setParentNum(parentNum);
|
||||
logManage.setPath(filePath +"/"+ file.getName());
|
||||
list.add(logManage);
|
||||
num++;
|
||||
// 需要加此判断。否则,ftp默认将‘项目文件所在目录之下的目录(./)’与‘项目文件所在目录向上一级目录下的目录(../)’都纳入递归,这样下去就陷入一个死循环了。需将其过滤掉。
|
||||
if (!".".equals(file.getName()) && !"..".equals(file.getName())) {
|
||||
findDirectory(ftp,list,num,filePath +"/"+ file.getName());
|
||||
ftp.changeToParentDirectory();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public InputStream downloadFTPFile(String localPath, String fileName){
|
||||
InputStream in = null;
|
||||
//传输模式
|
|
@ -16,12 +16,6 @@
|
|||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-base-core</artifactId>
|
||||
</dependency>
|
||||
<!--ftp依赖-->
|
||||
<dependency>
|
||||
<groupId>commons-net</groupId>
|
||||
<artifactId>commons-net</artifactId>
|
||||
<version>3.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -57,7 +57,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 = this.findDirectory(ftpClient, list, logManage.getOrderNum(), workPath + "/" + logManage.getName());
|
||||
ftpClient.changeToParentDirectory();
|
||||
}
|
||||
result.addAll(list);
|
||||
|
@ -69,6 +69,47 @@ public class LogManageController {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 遍历查询当前路径下的文件夹信息
|
||||
* @param ftp
|
||||
* @param list
|
||||
* @param filePath 以"/"开始和结束
|
||||
* @return
|
||||
*/
|
||||
public List<LogManage> findDirectory(FTPClient ftp,List<LogManage> list,Integer parentNum,String filePath){
|
||||
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());
|
||||
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.setParentNum(parentNum);
|
||||
logManage.setPath(filePath +"/"+ file.getName());
|
||||
list.add(logManage);
|
||||
num++;
|
||||
// 需要加此判断。否则,ftp默认将‘项目文件所在目录之下的目录(./)’与‘项目文件所在目录向上一级目录下的目录(../)’都纳入递归,这样下去就陷入一个死循环了。需将其过滤掉。
|
||||
if (!".".equals(file.getName()) && !"..".equals(file.getName())) {
|
||||
findDirectory(ftp,list,num,filePath +"/"+ file.getName());
|
||||
ftp.changeToParentDirectory();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前的文件夹转换成树形结构
|
||||
* @param logManages
|
||||
|
|
Loading…
Reference in New Issue
Block a user