Tree的工具类

该类通过JSP页面提供的文件路径地址,列出该文件路径下的所有文件,并以TREE的方式显示。

 

JSP调用类:

<%@page language="java" import="java.util.*" pageEncoding="gb2312"
 isELIgnored="false"%>


<%@page buffer="none"%>
<%@page import="java.sql.*"%>
<%@page import="com.util.js.*"%>
<%@page import="java.io.File"%>

<%@include file="../../../../include/inc/top.inc"%>
<%@include file="../../../../include/inc/tree.inc"%>
<head>

</head>

    <%
    
     String confPath = request.getRealPath("/conf").replace("\\","/");
     Tree tb = new Tree();
     tb.out=out;
     tb.session=session;
     tb.request =request;
     tb.title="";
     tb.root="conf";
     tb.printTree(confPath);
 
    %>

 

 

JAVA类:

package com.util.js;

import java.io.File;

public class Tree {

 public javax.servlet.jsp.JspWriter out = null;
 public javax.servlet.http.HttpSession session = null;
 public javax.servlet.http.HttpServletRequest request = null;
 public String title="";
 public String root="";
 
 public  void printTree(String FilePath)
 {
  try {
    File file = new File(FilePath);
    if(file.exists())
    {
     if("".equals(root)||root==null)
     {
      root="root";
     }
     out.println("<div id=\"main\">");
     out.println("<ul id=\"browser\" class=\"filetree treeview-famfamfam\">");
     out.println("<li>");
     out.println("<span class=\"folder\">"+root+"</span>");
     out.println("<ul>");
   
     listFile(file.listFiles());
     out.println("</ul>");
     out.println("</li>");
     out.println("</ul>");
     out.println("</div>");
    }
    else
    {
   out.println("<font color='red'>该目录下没有文件或文件夹不存在!</a>");
    }
  } catch (Exception e) {
   e.printStackTrace();
  }
  
 }
 public  void listFile(File[] files)
 {
  String path = request.getContextPath();
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  try {
   for (int i = 0; i < files.length; i++) {

    if (files[i].isDirectory())
    {
     
     out.println("<li class=\"closed\">");
     out.println("<span class=\"folder\">" + files[i].getName()+ "</span>");
     out.println("<ul>");
     listFile(files[i].listFiles());
     out.println("</ul>");
     out.println("</li>");
    }
    else
    {
     String fileAbsolute = files[i].getAbsolutePath();
     
     String filePath = fileAbsolute.toString().substring(fileAbsolute.toString().lastIndexOf(root),fileAbsolute.toString().length());
     filePath=filePath.replace("\\","/");
     //System.out.println("^^^"+filePath);
     out.println("<li>");
     out.println("<span class=\"file\"><a  onclick=\"window.open('"+basePath+filePath+"');\">"+files[i].getName()+"</a></span>");
     out.println("</li>");
     
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 public static void main(String[] args)
 {
   //File file = new File("E:\\Tomcat 6.0\\webapps\\BackUpFileAlarmSystem\\conf");
   //listFile(file.listFiles());
  
 }
}

你可能感兴趣的:(java,sql,tomcat,jsp,servlet)