写了一个类文件,读取指定目录下的文件列表,这个类文件单独编译没有问题,但是在调用的时候老是报下面的错误。
类文件: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>
<jsp:useBean id="f" class="FileViewer" scope="page" />
<%
f.setPath("C:\\Tomcat 5.0\\webapps\\myapp\\yuletiandi\\dianyingwenjianjia"); //C:\\Tomcat 5.0\\webapps\\myapp\\yuletiandi\\dianyingwenjianjia
f.refreshList();
while(f.nextFile()){
System.out.print(f.getFileName());
if(!f.getFileType())
System.out.print(" "+f.getFileSize());
else
System.out.print(" <DIR>");
System.out.print(f.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: Unable to compile class for JSPAn error occurred at line: 11 in the jsp file: /yuletiandi/listdianying.jsp
Generated servlet error:
C:\Tomcat 5.0\work\Catalina\localhost\myapp\org\apache\jsp\yuletiandi\listdianying_jsp.java:53: cannot access org.apache.jsp.yuletiandi.FileViewer
An error occurred at line: 11 in the jsp file: /yuletiandi/listdianying.jsp
Generated servlet error:
bad class file: C:\Tomcat 5.0\work\Catalina\localhost\myapp\org\apache\jsp\yuletiandi\FileViewer.java
file does not contain class org.apache.jsp.yuletiandi.FileViewer
Please remove or make sure it appears in the correct subdirectory of the classpath.
FileViewer f = null;
^
1 error org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:332)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:437)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:497)
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.
类文件: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>
<jsp:useBean id="f" class="FileViewer" scope="page" />
<%
f.setPath("C:\\Tomcat 5.0\\webapps\\myapp\\yuletiandi\\dianyingwenjianjia"); //C:\\Tomcat 5.0\\webapps\\myapp\\yuletiandi\\dianyingwenjianjia
f.refreshList();
while(f.nextFile()){
System.out.print(f.getFileName());
if(!f.getFileType())
System.out.print(" "+f.getFileSize());
else
System.out.print(" <DIR>");
System.out.print(f.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: Unable to compile class for JSPAn error occurred at line: 11 in the jsp file: /yuletiandi/listdianying.jsp
Generated servlet error:
C:\Tomcat 5.0\work\Catalina\localhost\myapp\org\apache\jsp\yuletiandi\listdianying_jsp.java:53: cannot access org.apache.jsp.yuletiandi.FileViewer
An error occurred at line: 11 in the jsp file: /yuletiandi/listdianying.jsp
Generated servlet error:
bad class file: C:\Tomcat 5.0\work\Catalina\localhost\myapp\org\apache\jsp\yuletiandi\FileViewer.java
file does not contain class org.apache.jsp.yuletiandi.FileViewer
Please remove or make sure it appears in the correct subdirectory of the classpath.
FileViewer f = null;
^
1 error org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:332)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:437)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:497)
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.
我的Web目录:
C:\Tomcat 5.0\webapps\myapp,FileViewer.java文件放在myapp下面的WEB-INF文件夹里