当前页面: 开发资料首页 → JSP 专题 → 在jsp用户怎么读取指定目录下的文件列表(急!!!要吐血了)
在jsp用户怎么读取指定目录下的文件列表(急!!!要吐血了)
摘要: 在jsp用户怎么读取指定目录下的文件列表(急!!!要吐血了)
各位大虾,帮帮我,写了一个读取指定目录下的文件的类,在jsp文件中调用,老是出错,请问怎么解决!下面是代码:
package yuletiandi
import java.io.File;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
public class FileViewer{
File myDir;
File[] contents;
Vector vectorList;
Iterator currentFileView;
File currentFile;
String path;
public FileViewer(){
path=new String("");
vectorList=new Vector();
}
public FileViewer(String path){
this.path=path;
vectorList=new Vector();
}
/**
* 设置浏览的路径
*/
public void setPath(String path){
this.path=path;
}
/***
* 返回当前目录路径
*/
public String getDirectory(){
return myDir.getPath();
}
/**
* 刷新列表
*/
public void refreshList(){
if(this.path.equals("")) path="c://";
myDir=new File(path);
vectorList.clear();
contents =myDir.listFiles();
//重新装入路径下文件
for(int i=0;i
vectorList.add(contents[i]);
}
currentFileView=vectorList.iterator();
}
/**
* 移动当前文件集合的指针指到下一个条目
* @return 成功返回true,否则false
*/
public boolean nextFile(){
while(currentFileView.hasNext()){
currentFile=(File)currentFileView.next();
return true;
}
return false;
}
/**
* 返回当前指向的文件对象的文件名称
*/
public String getFileName(){
return currentFile.getName();
}
/**
* 返回当前指向的文件对象的文件尺寸
*/
public String getFileSize(){
return new Long(currentFile.length()).toString();
}
/**
* 返回当前指向的文件对象的最后修改日期
*/
public String getFileTimeStamp(){
return new Date(currentFile.lastModified()).toString();
}
/**
* 返回当前指向的文件对象是否是一个文件目录
*/
public boolean getFileType(){
return currentFile.isDirectory();
}
}
下面是页面代码:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<head>
无标题文档</head>
<body>
电影列表
<%@ page import=" java.io.File,java.util.Date,java.util.Iterator,java.util.Vector" %>
<%
file.setPath("C://Tomcat 5.0//webapps//myapp//yuletiandi//dianyingwenjianjia");
file.refreshList();
while(file.nextFile()){
System.out.print(file.getFileName());
if(!file.getFileType())
System.out.print(" "+file.getFileSize());
else
System.out.print("
");
System.out.print(file.getFileTimeStamp()+"/n");
}
%>
</body>
运行后的结果:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: /yuletiandi/listdianying.jsp(12,0) The value for the useBean class attribute yuletiandi.FileViewer is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:409)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:150)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1227)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1116)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2213)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2219)
org.apache.jasper.compiler.Node$Root.accept(Node.java:456)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Generator.generate(Generator.java:3272)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:244)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:495)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:476)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:464)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.30 logs.
--------------------------------------------------------------------------------
求各位帮我看看!
File objFile = new File(strDir);
File[] list = objFile.listFiles();
list 里就是该文件夹下所有文件了。
我 Blog 里有篇 JSP资源管理器.里面有详细的方法.你可以看看。