<% /** 文件名: folder.jsp 描述: 一个简单的系统文件目录显示程序,类似于资源管理器,不过功能弱多了。通过这个例子主要是学习一下SYstem.IO.File类 。 作者: 慈勤强 Emai : [email protected] **/ %> <%@ page contentType="text/html;charset=gb2312"%> <%@page import="java.io.*" %> <style> td,select,input,body { font-size:9pt; } </style> <title>Jsp File System Viewer</title> <%! String getDrivers() /** Windows系统上取得可用的所有逻辑盘 **/ { StringBuffer sb=new StringBuffer("Drivers : "); File roots[]=File.listRoots(); for(int i=0;i<roots.length;i++) { sb.append("<a href='?path="+roots[i]+"'>"); sb.append(roots[i]+"</a> "); } return sb.toString(); } %> <% String strThisFile="folder.jsp"; request.setCharacterEncoding("gb2312"); String strDir = request.getParameter("path");
if(strDir==null||strDir.length()<1) { strDir = "c:\\"; } StringBuffer sb=new StringBuffer(""); StringBuffer sbFile=new StringBuffer(""); try { out.println("<table border=1 width='100%' bgcolor='#F1f1f1'><tr><td width='30%'>Current Directory: <b>"+strDir+"</b></td><td>" + getDrivers() + "</td></tr></table><br>"); File objFile = new File(strDir); File list[] = objFile.listFiles(); if(objFile.getAbsolutePath().length()>3) { sb.append("<a href='?path="+objFile.getParentFile().getAbsolutePath()+"'>"); sb.append("Parent Folder</a><p>"); } for(int i=0;i<list.length;i++) { if(list[i].isDirectory()) { sb.append("D <a href='?path="+list[i].getAbsolutePath()+"'>"+list[i].getName()+"</a><br>"); } else { sbFile.append("F "+list[i].getName()+"<br>"); } } out.println(sb.toString()+sbFile.toString()); } catch(Exception e) { out.println("<font color=red>"+e.toString()+"</font>"); } %> 
|