求助帮忙写个遍历文件夹,并在web页面上以树状结构显示文件列表,
可以的话每个文件加个点击事件显示文件路径
8条回答 默认 最新
- ApacheCrazyFan 2018-04-17 11:56关注
做的不是很好:
页面:
<!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset="UTF-8"> <title>文件目录</title> <link rel="stylesheet" href="static/bootstrap/css/bootstrap.min.css" /> <!--使用zTree风格--> <link rel="stylesheet" href="static/custom/fileCatalog/css/metroStyle/metroStyle.css" /> <script type="text/javascript" src="static/jquery/jquery-3.2.1.min.js"></script> <script type="text/javascript" src="static/bootstrap/js/bootstrap.min.js"></script> <script type="text/javascript" src="static/custom/fileCatalog/js/jquery.ztree.all.min.js"></script> <script type="text/javascript" src="static/custom/fileCatalog/fileCatalog.js"></script> </head> <body> <div> <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist"> <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li> </ul> <!-- Tab panes --> <div class="tab-content"> <div role="tabpanel" class="tab-pane active" id="home"> <ul class="ztree" id="menu"></ul> </div> </div> </div> </body> </html>
js:
/** * Created by wangbiao-019 on 2018/4/17. */ var setting = { view: { //addHoverDom: addHoverDom, //removeHoverDom: removeHoverDom, selectedMulti: false }, check: { enable: true }, data: { simpleData: { enable: true } }, edit: { enable: false }, callback: { onClick: zTreeOnClick } }; var zNodes; $.ajax({ url:"fileCatalog/fileCatalogs", type:"GET", dataType:"json", success:function (data) { zNodes = data; //console.info(zNodes); }, error:function (error) { console.info(error); } }); function concatParentsPath(currNode){ if(currNode.getParentNode() == null){ return currNode.name; }else{ return concatParentsPath(currNode.getParentNode() ) + currNode.name; } } function zTreeOnClick(e, treeId, treeNode){ var nodes = $.fn.zTree.getZTreeObj(treeId).getSelectedNodes(); var namePath = concatParentsPath(nodes[0]); $("#namePath").text(namePath); } function initZTree(id){ $.fn.zTree.init($(id), setting, zNodes); } $(document).ready(function () { initZTree("#menu"); }); //{ id: 1, pId: 0, name: "父节点1", open: true },
Controller:
package com.example.demo.on.controller; import com.example.demo.common.entity.Ztree; import com.example.demo.common.util.FileCatalogUtil; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.ArrayList; import java.util.List; /** * Created by wangbiao-019 on 2018/4/17. */ @Controller @RequestMapping("/fileCatalog") public class FileCatalogController { private List<Ztree.Node> nodes; @GetMapping("/fileCatalogs") @ResponseBody public List<Ztree.Node> getFileCatalogs() { nodes = new ArrayList<Ztree.Node>(); Ztree ztree = FileCatalogUtil.ergodicDeeply("D:/GitRespository/ideag/myspecial/src/main/java/com"); Ztree.Node root = ztree.getRoot(); mObj2Json(root); System.out.println( nodes); return nodes; } private void mObj2Json(Ztree.Node node){ nodes.add(node); recur(node); } private void recur(Ztree.Node node){ if(node != null && node.getSons() != null && node.getSons().size() > 0) { for (Ztree.Node n : node.getSons()) { nodes.add(n); recur(n); } } } }
model:
package com.example.demo.common.entity; import java.util.List; /** * Created by wangbiao-019 on 2018/4/17. */ public class Ztree { public class Node{ int id; int pId; String name; boolean open; //是否展开 boolean isParent; //是否是父节点 int fileCount; List<Node> sons; public Node(){ this.open = false; this.isParent = false; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getpId() { return pId; } public void setpId(int pId) { this.pId = pId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isOpen() { return open; } public void setOpen(boolean open) { this.open = open; } public boolean isParent() { return isParent; } public void setParent(boolean parent) { isParent = parent; } public int getFileCount() { return fileCount; } public void setFileCount(int fileCount) { this.fileCount = fileCount; } public List<Node> getSons() { return sons; } public void setSons(List<Node> sons) { this.sons = sons; } @Override public String toString() { return "{" + "\"id\":\"" + id + "\", \"pId\":\"" + pId + "\", \"name\":\"" + name + "\", \"open\":\"" + open + "\", \"isParent\":\"" + isParent + "\"}"; } } Node root; public Node getRoot() { return root; } public void setRoot(Node root) { this.root = root; } // }
util:
package com.example.demo.common.util;import com.example.demo.common.entity.Ztree;
import com.example.demo.common.entity.Ztree.Node;import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;/**
-
Created by wangbiao-019 on 2018/4/17.
*/
public class FileCatalogUtil {
private static int INT_START = 1;
private static final int INT_PARENT = 0;public static Ztree ergodicDeeply(String path) {
return ergodic(path, -1);
}//遍历文件夹的任意深度
// -1 表示会遍历到底,直至爆炸
// >0 表示遍历文件夹下的n层
// <0 return null;
public static Ztree ergodic(String path, int deepty) {
if (path == null || "".equals(path.trim()) || deepty == 0) {
return null;
} else {
Ztree ztree = new Ztree();
ztree.setRoot(nodefor(new File(path), ztree, deepty, INT_PARENT));
return ztree;
}
}private static Ztree.Node nodefor(File file, Ztree ztree, int deepth, int parentId) {
List nodes;
Node node;
if (file == null) {
return null;
} else if (deepth == 0) {
return null;
} else if (deepth == -1) {
if (file.exists()) {
node = ztree.new Node();node.setId(INT_START); node.setpId(parentId); try { if (file.isDirectory()) { //是目录 String path = null; path = file.getCanonicalPath(); node.setName(parentId == 0 ? path : path.substring(path.lastIndexOf(File.separator))); node.setParent(true); String[] fs = file.list(); if (fs == null) { return node; } else { nodes = new ArrayList<Node>(); for (String s : fs) { File fi = new File(file.getCanonicalPath(), s); INT_START++; nodes.add(nodefor(fi, ztree, deepth, node.getId())); } node.setSons(nodes); } } else if (file.isFile()) { //是文件 node.setName(file.getName()); INT_START++; } else if (file.isHidden()) { //是隐藏 node.setName(file.getName()); INT_START++; } else { //其他 node.setName(file.getName()); INT_START++; } } catch (IOException e) { e.printStackTrace(); } } else { return null; } } else if (deepth > 0) { return null; } else { return null; } return node;
}
}
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 -
悬赏问题
- ¥15 delta降尺度计算的一些细节,有偿
- ¥15 Arduino红外遥控代码有问题
- ¥15 数值计算离散正交多项式
- ¥30 数值计算均差系数编程
- ¥15 redis-full-check比较 两个集群的数据出错
- ¥15 Matlab编程问题
- ¥15 训练的多模态特征融合模型准确度很低怎么办
- ¥15 kylin启动报错log4j类冲突
- ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
- ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序