// 动态构造tree
public List<TreeVO> insertTree(String path) {
LinkedList<NamedNodeMap> folderList = new LinkedList();
LinkedList<NamedNodeMap> fileList = new LinkedList();
folderList = getfolderlist(path);// 传个路径,查出该路径下的所有文件夹
fileList = getfilelist(path);// 传个路径,查出该路径下的所有文件夹
folderList.addAll(fileList);// 文件夹+文件
List<TreeVO> lt = new ArrayList<TreeVO>();// 用来存放动态树的集合
for (NamedNodeMap namedNodeMap : folderList) {
String folderName = namedNodeMap.getNamedItem("label").getNodeValue();
String fpath = namedNodeMap.getNamedItem("path").getNodeValue();
String type = namedNodeMap.getNamedItem("type").getNodeValue();
TreeVO tv = new TreeVO();
tv.setId(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
tv.setText(folderName);
tv.setAttributes("{\"url\":\"/Portal/jqueryEasyUi/html/data.html\"}");
tv.setPath(fpath);
if ("folder".equalsIgnoreCase(type)) {
tv.setIconCls("icon-tree-folder");
tv.setChildren(insertTree(fpath)); // 递归 (从外往里)
}
lt.add(tv);
}
return lt;
}
// 写个方法,把最后的tree给转成json字符串
public String getJsonString(String path) {
List<TreeVO> lt = this.insertTree(path);
JSONArray ja = JSONArray.fromCollection(lt);
String js = ja.toString();
return js;
}
//TreeVO类
/**
*
* @author hou
*/
public class TreeVO {
private String id;
private String text;
private String attributes;
private String iconCls;
private String path;
private List<TreeVO> children;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getIconCls() {
return iconCls;
}
public void setIconCls(String iconCls) {
this.iconCls = iconCls;
}
public String getAttributes() {
return attributes;
}
public void setAttributes(String attributes) {
this.attributes = attributes;
}
public List<TreeVO> getChildren() {
return children;
}
public void setChildren(List<TreeVO> children) {
this.children = children;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}