亲测可用,留念
JSP代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="/common/jsp/mytag.jsp"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>文件树形展示</title>
<link rel="stylesheet" href="${webRoot}/plug-in/zTree/v3/css/demo.css" type="text/css">
<link rel="stylesheet" href="${webRoot}/plug-in/zTree/v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="${webRoot}/plug-in/zTree/v3/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="${webRoot}/plug-in/zTree/v3/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="${webRoot}/plug-in/zTree/v3/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript">
var setting = {
async: {
enable: true,
url:"${webRoot}/fileOperate/treedata",
autoParam:["id", "name", "level","pid","open","isParent","click","path"]
},
check: {
enable: true
},
view : {
dblClickExpand : false,
showLine : false,
selectedMulti: false
},
data : {
simpleData : {
enable : true
}
},
callback: {
beforeAsync: beforeAsync,
onAsyncError: onAsyncError,
onAsyncSuccess: onAsyncSuccess,
onCheck: onCheck
},
dataFilter: filter
};
function filter(treeId, parentNode, childNodes) {
console.log("filter:");console.log(parentNode);console.log(childNodes);
if (!childNodes) return null;
for (var i=0, l=childNodes.length; i<l; i++) {
childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
}
return childNodes;
}
//关键代码,通过treeNode遍历父亲节点,根节点再次调用getParentNode得到null终止循环
function getPathText(node){
var s=node.name;
while(node=node.getParentNode())s=node.name+'/'+s;
return s;
}
function showLog(str) {
console.log("showlog:"+str);
}
function onCheck(e, treeId, treeNode) {
showLog("[ "+getTime()+" onCheck ];" + getPathText(treeNode) );
}
var log, className = "dark";
function beforeAsync(treeId, treeNode) {
className = (className === "dark" ? "":"dark");
//showLog("[ "+getTime()+" beforeAsync ] " + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
return true;
}
function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
//showLog("[ "+getTime()+" onAsyncError ] " + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
}
function onAsyncSuccess(event, treeId, treeNode, msg) {
//showLog("[ "+getTime()+" onAsyncSuccess ] " + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
}
function getTime() {
var now= new Date(),
h=now.getHours(),
m=now.getMinutes(),
s=now.getSeconds(),
ms=now.getMilliseconds();
return (h+":"+m+":"+s+ " " +ms);
}
function refreshNode(e) {
var zTree = $.fn.zTree.getZTreeObj("tree"),
type = e.data.type,
silent = e.data.silent,
nodes = zTree.getSelectedNodes();
if (nodes.length == 0) {
alert("请先选择一个父节点");
}
for (var i=0, l=nodes.length; i<l; i++) {
zTree.reAsyncChildNodes(nodes[i], type, silent);
if (!silent) zTree.selectNode(nodes[i]);
}
}
$(document).ready(function(){
$.fn.zTree.init($("#tree"), setting);
});
</script>
</head>
<body>
<div class="content_wrap">
<div class="zTreeDemoBackground left">
<ul id="tree" class="ztree"></ul>
</div>
<div class="right">
</div>
</div>
</body>
</html>
java代码:
//树模型
public class TreeNode {
private String id;
/**
* pid,父节点的id
*/
private String pId;
/**
* 是否展开
*/
private boolean open;
/**
* 是否有子节点
*/
private boolean isParent;
/**
* 节点名称
*/
private String name;
/**
* 点击实践
*/
private String click;
/**
* 节点所指向的文件路径
*/
private String path;
public TreeNode(String id, String pid, boolean open, boolean isParent,
String name, String click, String path) {
this.id = id;
this.pId = pid;
this.open = open;
this.isParent = isParent;
this.name = name;
this.click = click;
this.path = path;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getpId() {
return pId;
}
public void setpId(String pId) {
this.pId = pId;
}
public boolean isOpen() {
return open;
}
public void setOpen(boolean open) {
this.open = open;
}
public boolean isIsParent() {
return isParent;
}
public void setParent(boolean isParent) {
this.isParent = isParent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClick() {
return click;
}
public void setClick(String click) {
this.click = click;
}
@Override
public String toString() {
return super.toString();
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
controller
/**
* 配合前端ztree,获取目录数据
* @param request
* @return
*/
@RequestMapping("/treedata")
@ResponseBody
public String treedata(HttpServletRequest request){
String id = request.getParameter("id");
String pid = request.getParameter("pid");
String open = request.getParameter("open");
String isParent = request.getParameter("isParent");
String click = request.getParameter("click");
String path = request.getParameter("path");
String name = request.getParameter("name");
String json = null;
if(null == name){
BuildTreeNodeUtils bu = new BuildTreeNodeUtils(ftpmappingdir);
json = bu.init().toString();
}else{
TreeNode tn = new TreeNode(id,pid, Boolean.parseBoolean(open),Boolean.parseBoolean(isParent), name,click, path);
BuildTreeNodeUtils bu = new BuildTreeNodeUtils(tn,true);
List<TreeNode> findChildNodes = bu.findChildNodes();
json = FastJsonUtils.toJSONString(findChildNodes);
}
//System.out.println(json);
return json;
}
BuildTreeNodeUtils:
public class BuildTreeNodeUtils {
/**
* 文件列表的工具类
*/
private FileListUtils fUtils;
/**
* 根结点
*/
private TreeNode headNode;
/**
* 父节点
*/
private TreeNode parentNode;
/**
* 文件列表
*/
private List<File> fList;
/**
* 目录列表
*/
private List<File> fDlist;
/**
*
*/
private boolean flag;
private String pid;
/**
* 是否有子节点
*/
private boolean isParent;
/**
* 目录树的数据
*/
private StringBuffer treeNodeData;
/**
* 文件路径
*/
private String path;
public BuildTreeNodeUtils(String path) {
this.path = path;
}
public BuildTreeNodeUtils(TreeNode tn, boolean flag) {
if (tn != null) {
fUtils = new FileListUtils(tn.getPath());
fList = fUtils.getfList();
fDlist = fUtils.getdList();
this.flag = flag;
this.pid = tn.getId();
this.parentNode = tn;
}
}
/**
* @description:构造一个根节点的json数据
* @return:StringBuffer
* @return
*/
public StringBuffer init() {
getHeadNode();
treeNodeData = new StringBuffer();
// {id:1, pId:0, name:"[core]", open:true},
treeNodeData.append("[{id:").append(headNode.getId()).append(",pid:").append(headNode.getpId()).append(",open:")
.append(headNode.isOpen()).append(",isParent:").append(headNode.isIsParent()).append(",name:\"")
.append(headNode.getPath().replaceAll("\\\\", "/")).append("\"").append(",click:\"")
.append(headNode.getClick()).append("\"").append(",path:\"")
.append(headNode.getPath().replaceAll("\\\\", "/")).append("\"").append("}]");
return treeNodeData;
}
/**
* @description:获得根节点
* @return:TreeNode
* @return
*/
public TreeNode getHeadNode() {
File ftemp = new File(path);
if (ftemp.isDirectory() && ftemp.listFiles().length > 0) {
headNode = new TreeNode("0", "0", false, true, ftemp.getPath(), "getChildNodes", ftemp.getPath());
} else {
headNode = new TreeNode("0", "0", false, false, ftemp.getPath(), "getChildNodes", ftemp.getPath());
}
return headNode;
}
/**
* @description: 获得某个节点的子节点
* @return:List<TreeNode>
* @return
*/
public List<TreeNode> findChildNodes() {
List<TreeNode> ltList = null;
if (parentNode.isIsParent()) {
ltList = new ArrayList<TreeNode>();
if (flag) {
for (int i = 0; i < fList.size(); i++) {
File file = new File(fList.get(i).getPath());
if (file.exists()) {
if (file.isDirectory()) {
isParent = true;
} else {
isParent = false;
}
}
TreeNode tNode;
if (isParent) {
tNode = new TreeNode(pid + i, pid, false, true, fList.get(i).getName(), "",fList.get(i).getPath());
} else {
tNode = new TreeNode(pid + i, pid, false, false, fList.get(i).getName(), "",fList.get(i).getPath());
}
ltList.add(tNode);
}
} else {
for (int i = 0; i < fDlist.size(); i++) {
TreeNode tNode;
tNode = new TreeNode(pid + i, pid, false, true, fDlist.get(i).getName(), "",
fDlist.get(i).getPath());
ltList.add(tNode);
}
}
}
return ltList;
}
/**
* @description: 构造子节点的json数据
* @return:StringBuffer
* @return
*/
public StringBuffer getTreeNodeData() {
List<TreeNode> ltList = findChildNodes();
int count = ltList.size();
if (count > 0) {
treeNodeData = new StringBuffer();
treeNodeData.append("[");
for (int i = 0; i < count; i++) {
TreeNode tempNode = ltList.get(i);
treeNodeData.append("{id:").append(tempNode.getId()).append(",pid:").append(tempNode.getpId())
.append(",open:").append(tempNode.isOpen()).append(",isParent:").append(tempNode.isIsParent())
.append(",name:").append("\"").append(tempNode.getName().replaceAll("\\\\", "/")).append("\"")
.append(",click:\"").append(tempNode.getClick()).append("\"").append(",path:").append("\"")
.append(tempNode.getPath().replaceAll("\\\\", "/")).append("\"").append("}");
if (i == count - 1) {
treeNodeData.append("]");
} else {
treeNodeData.append(",");
}
}
}
return treeNodeData;
}
public String getFileName() {
return parentNode.getName();
}
public FileListUtils getfUtils() {
return fUtils;
}
public TreeNode getParentNode() {
return parentNode;
}
public List<File> getfList() {
return fList;
}
public List<File> getfDlist() {
return fDlist;
}
public boolean isFlag() {
return flag;
}
public String getPid() {
return pid;
}
public boolean isParent() {
return isParent;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
FileListUtils:
public class FileListUtils {
private String path = "";
public FileListUtils(String path) {
this.path = path;
}
/**
* 所有文件列表
*
* @return
*/
public List<File> getfList() {
List<File> fileList = new ArrayList<File>();
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
//if (tempList[i].isFile()) {
//System.out.println("文件:" + tempList[i]);
fileList.add(tempList[i]);
//}
}
return fileList;
}
/**
* 文件夹列表
*
* @return
*/
public List<File> getdList() {
List<File> fileList = new ArrayList<File>();
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isDirectory()) {
//System.out.println("文件夹:" + tempList[i]);
fileList.add(tempList[i]);
}
}
return fileList;
}
}
JSP代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="/common/jsp/mytag.jsp"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>文件树形展示</title>
<link rel="stylesheet" href="${webRoot}/plug-in/zTree/v3/css/demo.css" type="text/css">
<link rel="stylesheet" href="${webRoot}/plug-in/zTree/v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="${webRoot}/plug-in/zTree/v3/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="${webRoot}/plug-in/zTree/v3/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="${webRoot}/plug-in/zTree/v3/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript">
var setting = {
async: {
enable: true,
url:"${webRoot}/fileOperate/treedata",
autoParam:["id", "name", "level","pid","open","isParent","click","path"]
},
check: {
enable: true
},
view : {
dblClickExpand : false,
showLine : false,
selectedMulti: false
},
data : {
simpleData : {
enable : true
}
},
callback: {
beforeAsync: beforeAsync,
onAsyncError: onAsyncError,
onAsyncSuccess: onAsyncSuccess,
onCheck: onCheck
},
dataFilter: filter
};
function filter(treeId, parentNode, childNodes) {
console.log("filter:");console.log(parentNode);console.log(childNodes);
if (!childNodes) return null;
for (var i=0, l=childNodes.length; i<l; i++) {
childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
}
return childNodes;
}
//关键代码,通过treeNode遍历父亲节点,根节点再次调用getParentNode得到null终止循环
function getPathText(node){
var s=node.name;
while(node=node.getParentNode())s=node.name+'/'+s;
return s;
}
function showLog(str) {
console.log("showlog:"+str);
}
function onCheck(e, treeId, treeNode) {
showLog("[ "+getTime()+" onCheck ];" + getPathText(treeNode) );
}
var log, className = "dark";
function beforeAsync(treeId, treeNode) {
className = (className === "dark" ? "":"dark");
//showLog("[ "+getTime()+" beforeAsync ] " + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
return true;
}
function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
//showLog("[ "+getTime()+" onAsyncError ] " + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
}
function onAsyncSuccess(event, treeId, treeNode, msg) {
//showLog("[ "+getTime()+" onAsyncSuccess ] " + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
}
function getTime() {
var now= new Date(),
h=now.getHours(),
m=now.getMinutes(),
s=now.getSeconds(),
ms=now.getMilliseconds();
return (h+":"+m+":"+s+ " " +ms);
}
function refreshNode(e) {
var zTree = $.fn.zTree.getZTreeObj("tree"),
type = e.data.type,
silent = e.data.silent,
nodes = zTree.getSelectedNodes();
if (nodes.length == 0) {
alert("请先选择一个父节点");
}
for (var i=0, l=nodes.length; i<l; i++) {
zTree.reAsyncChildNodes(nodes[i], type, silent);
if (!silent) zTree.selectNode(nodes[i]);
}
}
$(document).ready(function(){
$.fn.zTree.init($("#tree"), setting);
});
</script>
</head>
<body>
<div class="content_wrap">
<div class="zTreeDemoBackground left">
<ul id="tree" class="ztree"></ul>
</div>
<div class="right">
</div>
</div>
</body>
</html>
java代码:
//树模型
public class TreeNode {
private String id;
/**
* pid,父节点的id
*/
private String pId;
/**
* 是否展开
*/
private boolean open;
/**
* 是否有子节点
*/
private boolean isParent;
/**
* 节点名称
*/
private String name;
/**
* 点击实践
*/
private String click;
/**
* 节点所指向的文件路径
*/
private String path;
public TreeNode(String id, String pid, boolean open, boolean isParent,
String name, String click, String path) {
this.id = id;
this.pId = pid;
this.open = open;
this.isParent = isParent;
this.name = name;
this.click = click;
this.path = path;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getpId() {
return pId;
}
public void setpId(String pId) {
this.pId = pId;
}
public boolean isOpen() {
return open;
}
public void setOpen(boolean open) {
this.open = open;
}
public boolean isIsParent() {
return isParent;
}
public void setParent(boolean isParent) {
this.isParent = isParent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClick() {
return click;
}
public void setClick(String click) {
this.click = click;
}
@Override
public String toString() {
return super.toString();
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
controller
/**
* 配合前端ztree,获取目录数据
* @param request
* @return
*/
@RequestMapping("/treedata")
@ResponseBody
public String treedata(HttpServletRequest request){
String id = request.getParameter("id");
String pid = request.getParameter("pid");
String open = request.getParameter("open");
String isParent = request.getParameter("isParent");
String click = request.getParameter("click");
String path = request.getParameter("path");
String name = request.getParameter("name");
String json = null;
if(null == name){
BuildTreeNodeUtils bu = new BuildTreeNodeUtils(ftpmappingdir);
json = bu.init().toString();
}else{
TreeNode tn = new TreeNode(id,pid, Boolean.parseBoolean(open),Boolean.parseBoolean(isParent), name,click, path);
BuildTreeNodeUtils bu = new BuildTreeNodeUtils(tn,true);
List<TreeNode> findChildNodes = bu.findChildNodes();
json = FastJsonUtils.toJSONString(findChildNodes);
}
//System.out.println(json);
return json;
}
BuildTreeNodeUtils:
public class BuildTreeNodeUtils {
/**
* 文件列表的工具类
*/
private FileListUtils fUtils;
/**
* 根结点
*/
private TreeNode headNode;
/**
* 父节点
*/
private TreeNode parentNode;
/**
* 文件列表
*/
private List<File> fList;
/**
* 目录列表
*/
private List<File> fDlist;
/**
*
*/
private boolean flag;
private String pid;
/**
* 是否有子节点
*/
private boolean isParent;
/**
* 目录树的数据
*/
private StringBuffer treeNodeData;
/**
* 文件路径
*/
private String path;
public BuildTreeNodeUtils(String path) {
this.path = path;
}
public BuildTreeNodeUtils(TreeNode tn, boolean flag) {
if (tn != null) {
fUtils = new FileListUtils(tn.getPath());
fList = fUtils.getfList();
fDlist = fUtils.getdList();
this.flag = flag;
this.pid = tn.getId();
this.parentNode = tn;
}
}
/**
* @description:构造一个根节点的json数据
* @return:StringBuffer
* @return
*/
public StringBuffer init() {
getHeadNode();
treeNodeData = new StringBuffer();
// {id:1, pId:0, name:"[core]", open:true},
treeNodeData.append("[{id:").append(headNode.getId()).append(",pid:").append(headNode.getpId()).append(",open:")
.append(headNode.isOpen()).append(",isParent:").append(headNode.isIsParent()).append(",name:\"")
.append(headNode.getPath().replaceAll("\\\\", "/")).append("\"").append(",click:\"")
.append(headNode.getClick()).append("\"").append(",path:\"")
.append(headNode.getPath().replaceAll("\\\\", "/")).append("\"").append("}]");
return treeNodeData;
}
/**
* @description:获得根节点
* @return:TreeNode
* @return
*/
public TreeNode getHeadNode() {
File ftemp = new File(path);
if (ftemp.isDirectory() && ftemp.listFiles().length > 0) {
headNode = new TreeNode("0", "0", false, true, ftemp.getPath(), "getChildNodes", ftemp.getPath());
} else {
headNode = new TreeNode("0", "0", false, false, ftemp.getPath(), "getChildNodes", ftemp.getPath());
}
return headNode;
}
/**
* @description: 获得某个节点的子节点
* @return:List<TreeNode>
* @return
*/
public List<TreeNode> findChildNodes() {
List<TreeNode> ltList = null;
if (parentNode.isIsParent()) {
ltList = new ArrayList<TreeNode>();
if (flag) {
for (int i = 0; i < fList.size(); i++) {
File file = new File(fList.get(i).getPath());
if (file.exists()) {
if (file.isDirectory()) {
isParent = true;
} else {
isParent = false;
}
}
TreeNode tNode;
if (isParent) {
tNode = new TreeNode(pid + i, pid, false, true, fList.get(i).getName(), "",fList.get(i).getPath());
} else {
tNode = new TreeNode(pid + i, pid, false, false, fList.get(i).getName(), "",fList.get(i).getPath());
}
ltList.add(tNode);
}
} else {
for (int i = 0; i < fDlist.size(); i++) {
TreeNode tNode;
tNode = new TreeNode(pid + i, pid, false, true, fDlist.get(i).getName(), "",
fDlist.get(i).getPath());
ltList.add(tNode);
}
}
}
return ltList;
}
/**
* @description: 构造子节点的json数据
* @return:StringBuffer
* @return
*/
public StringBuffer getTreeNodeData() {
List<TreeNode> ltList = findChildNodes();
int count = ltList.size();
if (count > 0) {
treeNodeData = new StringBuffer();
treeNodeData.append("[");
for (int i = 0; i < count; i++) {
TreeNode tempNode = ltList.get(i);
treeNodeData.append("{id:").append(tempNode.getId()).append(",pid:").append(tempNode.getpId())
.append(",open:").append(tempNode.isOpen()).append(",isParent:").append(tempNode.isIsParent())
.append(",name:").append("\"").append(tempNode.getName().replaceAll("\\\\", "/")).append("\"")
.append(",click:\"").append(tempNode.getClick()).append("\"").append(",path:").append("\"")
.append(tempNode.getPath().replaceAll("\\\\", "/")).append("\"").append("}");
if (i == count - 1) {
treeNodeData.append("]");
} else {
treeNodeData.append(",");
}
}
}
return treeNodeData;
}
public String getFileName() {
return parentNode.getName();
}
public FileListUtils getfUtils() {
return fUtils;
}
public TreeNode getParentNode() {
return parentNode;
}
public List<File> getfList() {
return fList;
}
public List<File> getfDlist() {
return fDlist;
}
public boolean isFlag() {
return flag;
}
public String getPid() {
return pid;
}
public boolean isParent() {
return isParent;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
FileListUtils:
public class FileListUtils {
private String path = "";
public FileListUtils(String path) {
this.path = path;
}
/**
* 所有文件列表
*
* @return
*/
public List<File> getfList() {
List<File> fileList = new ArrayList<File>();
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
//if (tempList[i].isFile()) {
//System.out.println("文件:" + tempList[i]);
fileList.add(tempList[i]);
//}
}
return fileList;
}
/**
* 文件夹列表
*
* @return
*/
public List<File> getdList() {
List<File> fileList = new ArrayList<File>();
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isDirectory()) {
//System.out.println("文件夹:" + tempList[i]);
fileList.add(tempList[i]);
}
}
return fileList;
}
}