各位大虾,帮帮我,写了一个读取指定目录下的文件的类,在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<contents.length;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="" %><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head><body>
<p>电影列表</p>
<%@ page import=" java.io.File,java.util.Date,java.util.Iterator,java.util.Vector" %>
<jsp:useBean id="file" class="yuletiandi.FileViewer" scope="page" />
<%
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(" <DIR>");
System.out.print(file.getFileTimeStamp()+"\n");
}
%>
<p> </p>
<p> </p>
</body>
</html>运行后的结果:type Exception reportmessage 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.
--------------------------------------------------------------------------------
求各位帮我看看!
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<contents.length;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="" %><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head><body>
<p>电影列表</p>
<%@ page import=" java.io.File,java.util.Date,java.util.Iterator,java.util.Vector" %>
<jsp:useBean id="file" class="yuletiandi.FileViewer" scope="page" />
<%
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(" <DIR>");
System.out.print(file.getFileTimeStamp()+"\n");
}
%>
<p> </p>
<p> </p>
</body>
</html>运行后的结果:type Exception reportmessage 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.
--------------------------------------------------------------------------------
求各位帮我看看!
解决方案 »
- 数据排序问题?
- <c:if test> 如何判断两个类似否相等?
- Java中日期的使用方法
- 如何从数据库读取系统配置信息 以 代替从.properties中读取?
- 关于使用apache的DBUtils读取MySQL数据库数据的问题
- 我在配置servlet时遇到问题。麻烦您了……
- jsp初学者:请问username=new String(username.getBytes("ISO8859_1"),"GBK");为什么汉字要进行编码转换?
- 请问,如何给按钮增加颜色?
- 分页显示,如何实现数据库指针的重定位!
- 求教关于访问量的问题!
- 哪里下载hibernate软件??
- 用代码控制在客户端机器上创建文件的问题??急
File[] list = objFile.listFiles();list 里就是该文件夹下所有文件了。我 Blog 里有篇 JSP资源管理器.里面有详细的方法.你可以看看。