生成文件等目录结构方法(dTree js框架)

 

最近项目中需要向用户暴露一个目录,并向用户友好得展示出来,所以就选择了dTree这个js框架,地址:

http://www.destroydrop.com/javascripts/tree/dtree.zip

这里用struts2结合dtree展示目录结构,部分代码如下:

DtreeUtil.java

 

Java代码   
  1. package com.coship.util;   
  2.   
  3. import java.io.File;   
  4.   
  5. public class DtreeUtil {   
  6.     private static int globalNum = 1;   
  7.   
  8.     private static final int ROOT_ID = 0;   
  9.   
  10.     private static final int ROOT_PID = -1;   
  11.   
  12.     private static final String FILE_SEPARATOR = "/";   
  13.   
  14.     private static final String LINE_BREAK = "\n";   
  15.   
  16.     public static String getDtreeCode(String rootDirPath) {   
  17.         StringBuffer buffer = new StringBuffer();   
  18.         buffer.append("d = new dTree('d');" + LINE_BREAK);   
  19.         buffer.append("d.add(" + ROOT_ID + "," + ROOT_PID + ",'" + rootDirPath   
  20.                 + "','" + rootDirPath + "');" + LINE_BREAK);   
  21.         DtreeUtil.recursive(rootDirPath, ROOT_ID, buffer);   
  22.         buffer.append("document.write(d);" + LINE_BREAK);   
  23.         String dTreeCode = buffer.toString();   
  24.   
  25.         return dTreeCode;   
  26.     }   
  27.   
  28.     private static void recursive(String dirPath, int pid, StringBuffer buffer) {   
  29.         File curDir = new File(dirPath);   
  30.   
  31.         String[] nameOfFiles = curDir.list();   
  32.   
  33.         for (String nameOfFile : nameOfFiles) {   
  34.             String curFilePath = dirPath + FILE_SEPARATOR + nameOfFile;   
  35.             int id = globalNum++;   
  36.             File curFile = new File(curFilePath);   
  37.             buffer.append("d.add(" + id + "," + pid + ",'" + nameOfFile + "','"  
  38.                     + curFilePath + "');" + LINE_BREAK);   
  39.             if (curFile.isDirectory()) {   
  40.                 DtreeUtil.recursive(curFilePath, id, buffer);   
  41.             }   
  42.         }   
  43.     }   
  44.   
  45.     public static void main(String[] args) {   
  46.         String rootDirPath = "E:/study";   
  47.         String dTreeCode = DtreeUtil.getDtreeCode(rootDirPath);   
  48.         System.out.println(dTreeCode);   
  49.     }   
  50. }  
package com.coship.util;

import java.io.File;

public class DtreeUtil {
	private static int globalNum = 1;

	private static final int ROOT_ID = 0;

	private static final int ROOT_PID = -1;

	private static final String FILE_SEPARATOR = "/";

	private static final String LINE_BREAK = "\n";

	public static String getDtreeCode(String rootDirPath) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("d = new dTree('d');" + LINE_BREAK);
		buffer.append("d.add(" + ROOT_ID + "," + ROOT_PID + ",'" + rootDirPath
				+ "','" + rootDirPath + "');" + LINE_BREAK);
		DtreeUtil.recursive(rootDirPath, ROOT_ID, buffer);
		buffer.append("document.write(d);" + LINE_BREAK);
		String dTreeCode = buffer.toString();

		return dTreeCode;
	}

	private static void recursive(String dirPath, int pid, StringBuffer buffer) {
		File curDir = new File(dirPath);

		String[] nameOfFiles = curDir.list();

		for (String nameOfFile : nameOfFiles) {
			String curFilePath = dirPath + FILE_SEPARATOR + nameOfFile;
			int id = globalNum++;
			File curFile = new File(curFilePath);
			buffer.append("d.add(" + id + "," + pid + ",'" + nameOfFile + "','"
					+ curFilePath + "');" + LINE_BREAK);
			if (curFile.isDirectory()) {
				DtreeUtil.recursive(curFilePath, id, buffer);
			}
		}
	}

	public static void main(String[] args) {
		String rootDirPath = "E:/study";
		String dTreeCode = DtreeUtil.getDtreeCode(rootDirPath);
		System.out.println(dTreeCode);
	}
}

 

DtreeAction.java

 

Java代码 复制代码  收藏代码
  1. package com.coship.action;   
  2.   
  3. import com.coship.util.DtreeUtil;   
  4. import com.opensymphony.xwork2.ActionSupport;   
  5.   
  6. public class DtreeAction extends ActionSupport {   
  7.     private static final long serialVersionUID = 1L;   
  8.   
  9.     private String path;   
  10.   
  11.     private String dTreeCode;   
  12.   
  13.     public String getdTreeCode() {   
  14.         return dTreeCode;   
  15.     }   
  16.   
  17.     public void setdTreeCode(String dTreeCode) {   
  18.         this.dTreeCode = dTreeCode;   
  19.     }   
  20.   
  21.     public String getPath() {   
  22.         return path;   
  23.     }   
  24.   
  25.     public void setPath(String path) {   
  26.         this.path = path;   
  27.     }   
  28.   
  29.     public String dtree() {   
  30.         this.dTreeCode = DtreeUtil.getDtreeCode(this.path);   
  31.         return SUCCESS;   
  32.     }   
  33.   
  34. }  
package com.coship.action;

import com.coship.util.DtreeUtil;
import com.opensymphony.xwork2.ActionSupport;

public class DtreeAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private String path;

	private String dTreeCode;

	public String getdTreeCode() {
		return dTreeCode;
	}

	public void setdTreeCode(String dTreeCode) {
		this.dTreeCode = dTreeCode;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String dtree() {
		this.dTreeCode = DtreeUtil.getDtreeCode(this.path);
		return SUCCESS;
	}

}

 

dTree.jsp

 

Jsp代码 复制代码  收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>   
  3. <%@ taglib prefix="s" uri="/struts-tags"%>   
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
  6. <html>   
  7. <head>   
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
  9. <title>Dtree</title>   
  10. <link rel="StyleSheet" href="dtree.css" type="text/css" />   
  11. <script type="text/javascript" src="dtree.js"></script>   
  12. </head>   
  13. <body>   
  14. <div class="dtree">   
  15.   
  16. <p><a href="javascript: d.openAll();">open all</a> | <a   
  17.     href="javascript: d.closeAll();">close all</a></p>   
  18.   
  19. <script type="text/javascript">   
  20. <!--   
  21.     <s:property value="dTreeCode" />   
  22. //-->   
  23. </script></div>   
  24. </body>   
  25. </html>  

 在第一个页面输入要展示的目录路径:



 提交后,将展示目录树结构:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值