usebean包(自己定义的,在src文件夹下面)里的java文件
FileAccept.java
package usebean; import java.io.File;
import java.io.FilenameFilter; public class FileAccept implements FilenameFilter{
private String extendName;
public void setExtendName(String s){
this.extendName="."+s;
}
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(extendName);
} }
browserFile.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@page import="java.io.*" %>
<jsp:useBean id="use" scope="page" class="usebean.FileAccept"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'browserFile.jsp' starting page</title>
</head> <body>
<font style="font-weight:bold;font-family:Microsoft Yahei">文件浏览</font><br>
<%
int fcount=0,dcount=0; path=request.getRealPath("");
File d=new File(path);
File list[]=d.listFiles(); /*
//返回特定的文件
//use就是一个对象,相当于FileAccept use=new FileAccept();
use.setExtendName("jsp");
String list[]=d.list(use);
for(int i=0;i<list.length;i++){
out.print(list[i]+"<br>");
}
*/ /*
String list[]=d.list();//以字符串的形式返回所有目录和文件名
for(int i=0;i<list.length;i++){
out.print(list[i]+"<br>");
}
*/ //输出文件
out.print(path+"目录下的文件:"+"<br>");
for(int i=0;i<list.length;i++){
if(list[i].isFile()){
out.print(list[i].getName()+"<br>");
fcount++;
}
} //输出目录
out.print("<br>"+path+"目录下的目录:"+"<br>");
for(int i=0;i<list.length;i++){
if(list[i].isDirectory()){
out.print(list[i].getName()+"<br>");
dcount++;
}
} %> <h3 style="color:blue">统计结果:</h3>
<center>
文件总数: <%=fcount %><br>
目录总数: <%=dcount %>
</center> </body>
</html>