1.文件树结构
"文件树")
(
value
=
JsonInclude.
Include.
NON_NULL)
public
class
BaseTreeNodeVO {
(
value
=
"文件id",
required
=
true)
protected
Long
id;
(
value
=
"父id",
required
=
true)
protected
Long
parentId;
(
value
=
"文件夹标识",
required
=
true)
protected
Boolean
isDirectory;
(
value
=
"文件名",
required
=
true)
private
String
name;
(
value
=
"子节点",
required
=
true)
protected
List
<
BaseTreeNodeVO
>
children
=
new
ArrayList
<>();
public
void
appendChild(
BaseTreeNodeVO
organTree){
this.
children.
add(
organTree);
}
}
(
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
2.获取文件树
public
BaseTreeNodeVO
getFileTree(
String
filePath){
long
parentId
=
1;
//初始化父节点id
long
id
=
1;
//初始化父节点id
List
<
FileTree
>
list
=
new
ArrayList
<>();
try {
FileUtil.
file(
id,
filePath,
parentId,
list);
}
catch (
FileNotFoundException
e) {
e.
printStackTrace();
}
BaseTreeNodeVO
bt
=
FileUtil.
assembleTree(
list);
bt
=
FileUtil.
sort(
bt);
LOG.
info(
LogProperty.
LOGTYPE_DETAIL,
JSON.
toJSONString(
bt));
return
bt;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
3.转换为list
public
static
List
<
FileTree
>
file(
Long
id,
String
filePath,
long
parentId,
List
<
FileTree
>
treeList)
throws
FileNotFoundException {
File
file
=
new
File(
filePath);
//1.判断文件
if(
!
file.
exists()){
throw
new
FileNotFoundException(
"文件不存在");
}
//3.获取文件夹路径下面的所有文件递归调用;
if(
file.
isDirectory()){
String
path
=
file.
getAbsolutePath();
String
name
=
file.
getName();
FileTree
fileTree
=
new
FileTree(
id
++,
name,
path,
parentId,
true);
treeList.
add(
fileTree);
String[]
list
=
file.
list();
for (
int
i
=
0;
i
<
list.
length;
i
++){
String
s
=
list[
i];
String
newFilePath
=
path
+
File.
separator
+
s;
//根据当前文件夹,拼接其下文文件形成新的路径
file(
id
++,
newFilePath,
fileTree.
getId(),
treeList);
}
}
//2.是文件该怎么执行
if(
file.
isFile()){
LOG.
info(
LogProperty.
LOGTYPE_DETAIL,
"is file");
String
name
=
file.
getName();
String
path
=
file.
getAbsolutePath();
FileTree
fileTree
=
new
FileTree(
id
++,
name,
path,
parentId,
false);
treeList.
add(
fileTree);
return
treeList;
}
return
treeList;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
4.转换为tree
public
static
<
T
extends
BaseTreeNodeVO
>
T
assembleTree(
List
<
T
>
list) {
if (
list.
isEmpty()) {
return
null;
}
//取出根节点
T
root
=
list.
get(
0);
//临时存放所有节点
Map
<
Long,
T
>
nodeMap
=
new
HashMap
<>();
list.
stream().
forEach(
o
->
nodeMap.
put(
o.
getId(),
o));
//找出节点关系
list.
subList(
1,
list.
size()).
stream().
forEach(
m
->
nodeMap.
get(
m.
getParentId()).
appendChild(
m));
list.
stream().
sorted(
Comparator.
comparing(
BaseTreeNodeVO::
getIsDirectory)).
collect(
Collectors.
toList());
nodeMap.
clear();
return
root;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
5.排序
public
static
BaseTreeNodeVO
sort(
BaseTreeNodeVO
bt) {
List
<
BaseTreeNodeVO
>
list
=
bt.
getChildren();
while (
!
list.
isEmpty()) {
List
<
BaseTreeNodeVO
>
dir
=
list.
stream().
filter(
a
->
a.
getIsDirectory()).
collect(
Collectors.
toList());
Collections.
sort(
dir,
Comparator.
comparing(
BaseTreeNodeVO::
getName));
List
<
BaseTreeNodeVO
>
file
=
list.
stream().
filter(
a
->
!
a.
getIsDirectory()).
collect(
Collectors.
toList());
Collections.
sort(
file,
Comparator.
comparing(
BaseTreeNodeVO::
getName));
for (
BaseTreeNodeVO
btChildren :
dir) {
sort(
btChildren);
}
dir.
addAll(
file);
bt.
setChildren(
dir);
return
bt;
}
return
bt;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.