首页 > 编程 > JSP > 正文

JSP页面文件目录树源码(递归算法)

2024-09-05 00:20:18
字体:
来源:转载
供稿:网友
<%@ page contenttype="text/html; charset=gb2312" language="java" import="java.sql.*" errorpage="" %>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html;charset=gb2312"; >
<script language="javascript">
<!--
function mm_gotourl() { //v3.0
var i, args=mm_gotourl.arguments; document.mm_returnvalue = false;
for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
</script>
<link href="css/ip.css" rel="stylesheet" type="text/css">
</head>

<body topmargin = "0" leftmargin = "0" marginheight = "0" marginwidth = "0" bgcolor="#ffffff">
<table border="0" cellspacing="0" cellpadding="0" height="80%">
<tr><td>
</td>
</tr>
<tr valign="top">
<td height="223"> <span id="menus"></span>
<span id="view"></span>

</td>
</tr>
<tr valign="bottom">
<td>&nbsp;</td>
</tr>
</table>
<div align="right"></div>
</body>
<script>
/**
* 构造树,初值为0
*/
function isbrother()
{
var is=flase;

}
function tree(n) {
var id = new array("bar","pad","<img src='http://www.VeVb.com/htmldata/2005-08-02/images/midnodeline.gif'border=0 align='absmiddle'><img src='http://www.VeVb.com/htmldata/2005-08-02/images/close.gif 'border=0 align='absmiddle'>","<img src='http://www.VeVb.com/htmldata/2005-08-02/images/midclosedfolder.gif 'border=0 align='absmiddle'><img src='http://www.VeVb.com/htmldata/2005-08-02/images/close.gif 'border=0 align='absmiddle'>","<img src='http://www.VeVb.com/htmldata/2005-08-02/images/lastnodeline.gif'border=0 align='absmiddle'><img src='http://www.VeVb.com/htmldata/2005-08-02/images/close.gif 'border=0 align='absmiddle'>");
if(n == 0) { // 初始化变量
n = 1;
i = 0;
s = "";
}
s += "<table border=0 cellspacing=0 cellpadding=0 >";
for(;i<tree_ar.length-1;i++) {
var k = (n >= tree_ar[i+1][0])?0:1;
if(tree_ar[i][2]==false)
{
id[2]="<img src='http://www.VeVb.com/htmldata/2005-08-02/images/lastnodeline.gif'border=0 align='absmiddle'><img src='http://www.VeVb.com/htmldata/2005-08-02/images/close.gif 'border=0 align='absmiddle'>";
}
s += "<tr id='"+id[k]+"' value="+i+"><td >"+id[k+2]+"</td><td>"+tree_ar[i][1]+"</td></tr>"; // // 构造节点,注意这里的自定义属性value。作用是简化构造节点的描述,共享参数数组信息。
if(n > tree_ar[i+1][0]) { // 若期望层次大于当前层次,结束本层次返回上一层次。
s += "</td></tr></table>";
return tree_ar[i+1][0];
}
if(n < tree_ar[i+1][0]) { // 若期望层次小于当前层次,递归进入下一层次。
s += "<tr style='display:none' ><td ";
if(tree_ar[i][2]==true)
{ //如果有下个兄弟节点,则画兄弟节点间的连线
s+="background='images/line.gif' border=0 align='absmiddle'";
}

s+="></td><td>";
var m = tree(tree_ar[++i][0]);
s += "</td></tr>";
if(m < n) { // 当递归返回值小于当前层次期望值时,将产生连续的返回动作
s += "</table>";
return m;
}
}
}
s += "</table>";
return s;
}
</script>

<script for=pad event=onclick>
// 分枝节点的点击响应
v = this.parentelement.rows[this.rowindex+1].style;
if(v.display == 'block') {
v.display = 'none';
this.cells[0].innerhtml = "<img src='http://www.VeVb.com/htmldata/2005-08-02/images/midclosedfolder.gif 'border=0 align='absmiddle'><img src='http://www.VeVb.com/htmldata/2005-08-02/images/close.gif 'border=0 align='absmiddle'>";
view.innerhtml = "";// 自行修改为参数数组定义的闭合动作
}else {
v.display = 'block';
this.cells[0].innerhtml = "<img src='http://www.VeVb.com/htmldata/2005-08-02/images/midopenedfolder.gif 'border=0 align='absmiddle'><img src='http://www.VeVb.com/htmldata/2005-08-02/images/open.gif 'border=0 align='absmiddle'>";
// view.innerhtml = "<b>"+tree_ar[this.value][1]+"</b>"; // 自行修改为参数数组定义的展开动作
}

/**
* 以下代码用于关闭已展开的其他分枝
* 如需自行关闭展开的分枝则从这里直接返回或删去这段代码
*/
if(! tree_ar[this.value].type) //如该节点为首次进入,则记录所在层次信息
gentreeinfo(this);
var n = 1*this.value+1;
for(i=n;i<tree_ar.length-1;i++) { // 关闭排列在当前节点之后的树
if(tree_ar[i].type == "pad") {
tree_ar[i].obj2.style.display = 'none';
tree_ar[i].obj1.cells[0].innerhtml = "<img src='http://www.VeVb.com/htmldata/2005-08-02/images/midclosedfolder.gif 'border=0 align='absmiddle'><img src='http://www.VeVb.com/htmldata/2005-08-02/images/close.gif 'border=0 align='absmiddle'>";
}
}
while(tree_ar[--n][0] > 1); //回溯到当前树的起点
while(--n >= 0) // 关闭排列在当前树的起点之前的树
if(tree_ar[n].type == "pad") {
tree_ar[n].obj2.style.display = 'none';
tree_ar[n].obj1.cells[0].innerhtml = "<img src='http://www.VeVb.com/htmldata/2005-08-02/images/midclosedfolder.gif 'border=0 align='absmiddle'><img src='http://www.VeVb.com/htmldata/2005-08-02/images/open.gif 'border=0 align='absmiddle'>";
}


/** 记录层次信息,用以简化遍历树时的复杂的节点描述 **/
function gentreeinfo(o) {
var el = o.parentelement;
for(var i=0;i<el.rows.length;i++) {
if(el.rows[i].id != "") {
tree_ar[el.rows[i].value].type = el.rows[i].id;
}
if(el.rows[i].id == "pad") {
tree_ar[el.rows[i].value].obj1 = el.rows[i];
tree_ar[el.rows[i].value].obj2 = el.rows[i+1];
}
}
}
</script>

<script for=bar event=onclick>
// 无分枝节点的点击响应
//view.innerhtml = "<b>"+tree_ar[this.value][1]+"</b>"; // // 自行修改为参数数组定义的点击动作
// 自行修改为参数数组定义的点击动作

</script>


<script for=bar event=onclick>
// 无分枝节点的点击响应
//view.innerhtml = "<b>"+tree_ar[this.value][1]+"</b>"; // /ufffd/ufffd/ufffds/ufffdc/ufffd/ufffd?/ufffdq/ufffd/ufffd/ufffd/ufffd?/ufffd/ufffd?/ufffdi/ufffd_??/ufffd/ufffd
</script>

<script>
/**
* 基本参数数组,根据具体应用自行扩展
* 数据可较简单的由服务器端提供
* 列1:节点层次
* 列2:节点标题
* 列3: 有无下个兄弟节点
* 其余自行扩充
*/
tree_ar = new array(

<%

filetree tree=new filetree();
string array=tree.showfiletree("e://");

%>
<%=array%>
new array(1,"",false) // 为简化终止判断附加的空数据项
);


/*** 创建菜单 ***/
menus.innerhtml =tree(0);
</script>
</html>





import java.io.file;

public class filetree {
public filetree() {
}

/**
*
* 生成文件目录树
*
* @param path
* 目录路径
* @return 返回jsp 页面所需的目录数组(javascript 数组)
*
*/
/*
* 有无兄弟节点
*/
private boolean hasnexetbrother = false;
/*
* 节点层次
*/
private int index=1;

public string showfiletree(string path) {

string s = "";
file dir = new file(path);
if (dir != null && dir.isdirectory()) {
string absolutepath = dir.getabsolutepath();


absolutepath = absolutepath.replaceall("////", "////////");//对'/'的处理,在jsp参数传递时'/'和空格会出现错误
absolutepath = absolutepath.replaceall(" ", "@"); //对空格的处理 ,这时用'@'替代
s += "new array(" + index + ",'<a href=filelist.jsp?path="
+ absolutepath + "//// target=mainframe>" + dir.getname()
+ "</a>'," + hasnexetbrother + "), ";
hasnexetbrother = false;
index++;
file[] dirs = dir.listfiles();
java.util.list list = new java.util.arraylist();
for (int i = 0; i < dirs.length; i++) {
if (dirs[i].isdirectory()) {//去除文件.得到文件夹列表

list.add(dirs[i]);

}
}
for (int i = 0; i < list.size(); i++) {
if (i != list.size() - 1)
hasnexetbrother = true;
string dirpath = ((file) list.get(i)).getabsolutepath();
s += showfiletree(dirpath);//对子文件夹递归处理

}
index--;
}

return s;
}
}
中国最大的web开发资源网站及技术社区,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表