我从网上下了一个用smartupload上传的代码,我觉得代码应该没问题,大家可以看看,共有upload.htm和upfile.jsp两个文件。其中upload.htm中的代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
BODY{
font-size:9pt
}
-->
</style><SCRIPT language=javascript>
function check_file()
{
var strFileName=form1.FileName.value;
if (strFileName=="")
{
alert("请选择要上传的文件");
return false;
}
}
</SCRIPT>
</head>
<body>
<form action="upfile.jsp" method="post" name="form1" onSubmit="return check_file()" enctype="multipart/form-data">
<input name="FileName" type="FILE" size="20">
<input type="submit" name="Submit" value="上传">
</form>
</body>
</html>而upfile.jsp中的代码为:<%@ page contentType="text/html;charset=GB2312"
import="com.jspsmart.upload.*,
java.util.*,
java.lang.*"
%><jsp:useBean id="mu" scope="page" class="com.jspsmart.upload.SmartUpload" /><HTML>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<BODY>
<%
// 初始化
final String UpFileType="zip|rar|doc|txt"; //上传文件类型
final int MaxFileSize=10240; //上传文件大小限制
String ErrMsg=null; //错误信息
boolean ERR=false; //错误标志
mu.initialize(pageContext);// 上传文件
mu.upload();// 取当前文件
com.jspsmart.upload.File myFile = mu.getFiles().getFile(0);// 取得文件并保存
if (!myFile.isMissing()) {
//得到文件扩展名
String FileType=myFile.getFileExt();
FileType=FileType.toLowerCase(); //将扩展名转换成小写
if (UpFileType.indexOf(FileType)==-1){
ERR=true;
ErrMsg="上传文件失败!目前只允许上传以下格式的文件:"+UpFileType;
}
//得到文件大小
int FileSize=myFile.getSize();
if (ERR==false&&FileSize>MaxFileSize){
ERR=true;
ErrMsg="上传文件失败!文件大小超出了限定的范围(最大"+MaxFileSize/1024+"K)";
}
if(ERR==false){
//保存文件
String newFileName="001."+FileType; //可自动生成文件名以防止同名覆盖
myFile.saveAs(newFileName);
}else{
out.print ("<script>alert('"+ErrMsg+"');</script>");
}
}
%>
</BODY>
</HTML>我的环境变量的配置是:
CLASSPATH: C:\jdk\lib\tools.jar;C:\jdk\lib\dt.jar;C:\tomcat\common\lib\servlet-api.jar
Path: C:\jdk\bin
TOMCAT_HOME: C:\tomcat
JAVA_HOME: C:\jdksmartupload的类文件放到了当前目录的WEB-INF下的classes中,顺序如:com.jspsmart.upload,好像也没什么问题,不过运行时出现了以下错误:type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: javax/servlet/jsp/PageContext
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.demo.upfile_jsp._jspService(upfile_jsp.java:115)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
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)
root cause java.lang.NoClassDefFoundError: javax/servlet/jsp/PageContext
com.jspsmart.upload.SmartUpload.initialize(SmartUpload.java:153)
org.apache.jsp.demo.upfile_jsp._jspService(upfile_jsp.java:75)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
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.28 logs.
--------------------------------------------------------------------------------Apache Tomcat/5.0.28
想过了许多办法,也下了其它类似的上传程序试过,都是同样的错误。不过其他的jsp程序都能正常运行。我也不知道究竟什么地方出了差错,难道是什么文件的配置有误或是我的jsk,tomcat的版本出了问题
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
BODY{
font-size:9pt
}
-->
</style><SCRIPT language=javascript>
function check_file()
{
var strFileName=form1.FileName.value;
if (strFileName=="")
{
alert("请选择要上传的文件");
return false;
}
}
</SCRIPT>
</head>
<body>
<form action="upfile.jsp" method="post" name="form1" onSubmit="return check_file()" enctype="multipart/form-data">
<input name="FileName" type="FILE" size="20">
<input type="submit" name="Submit" value="上传">
</form>
</body>
</html>而upfile.jsp中的代码为:<%@ page contentType="text/html;charset=GB2312"
import="com.jspsmart.upload.*,
java.util.*,
java.lang.*"
%><jsp:useBean id="mu" scope="page" class="com.jspsmart.upload.SmartUpload" /><HTML>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<BODY>
<%
// 初始化
final String UpFileType="zip|rar|doc|txt"; //上传文件类型
final int MaxFileSize=10240; //上传文件大小限制
String ErrMsg=null; //错误信息
boolean ERR=false; //错误标志
mu.initialize(pageContext);// 上传文件
mu.upload();// 取当前文件
com.jspsmart.upload.File myFile = mu.getFiles().getFile(0);// 取得文件并保存
if (!myFile.isMissing()) {
//得到文件扩展名
String FileType=myFile.getFileExt();
FileType=FileType.toLowerCase(); //将扩展名转换成小写
if (UpFileType.indexOf(FileType)==-1){
ERR=true;
ErrMsg="上传文件失败!目前只允许上传以下格式的文件:"+UpFileType;
}
//得到文件大小
int FileSize=myFile.getSize();
if (ERR==false&&FileSize>MaxFileSize){
ERR=true;
ErrMsg="上传文件失败!文件大小超出了限定的范围(最大"+MaxFileSize/1024+"K)";
}
if(ERR==false){
//保存文件
String newFileName="001."+FileType; //可自动生成文件名以防止同名覆盖
myFile.saveAs(newFileName);
}else{
out.print ("<script>alert('"+ErrMsg+"');</script>");
}
}
%>
</BODY>
</HTML>我的环境变量的配置是:
CLASSPATH: C:\jdk\lib\tools.jar;C:\jdk\lib\dt.jar;C:\tomcat\common\lib\servlet-api.jar
Path: C:\jdk\bin
TOMCAT_HOME: C:\tomcat
JAVA_HOME: C:\jdksmartupload的类文件放到了当前目录的WEB-INF下的classes中,顺序如:com.jspsmart.upload,好像也没什么问题,不过运行时出现了以下错误:type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: javax/servlet/jsp/PageContext
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.demo.upfile_jsp._jspService(upfile_jsp.java:115)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
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)
root cause java.lang.NoClassDefFoundError: javax/servlet/jsp/PageContext
com.jspsmart.upload.SmartUpload.initialize(SmartUpload.java:153)
org.apache.jsp.demo.upfile_jsp._jspService(upfile_jsp.java:75)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
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.28 logs.
--------------------------------------------------------------------------------Apache Tomcat/5.0.28
想过了许多办法,也下了其它类似的上传程序试过,都是同样的错误。不过其他的jsp程序都能正常运行。我也不知道究竟什么地方出了差错,难道是什么文件的配置有误或是我的jsk,tomcat的版本出了问题
解决方案 »
- 往access数据库里插入韩文为什么是乱码?
- 用activex以后,每次打开网页都要下载cab包,怎么解决?
- Struts中动态Form如何继承另一个动态Form
- 一个非常奇怪的问题,java下运行取sql server数据库datetime型数据可以,jsp不行。我搞了一天也没搞清楚。晕了!
- 有错吗?运行提示:can't find the declaration of element beans"
- javaBean和JSP间的数据传递
- 为什么???
- websphere下的servlet配置问题,急!
- 第一次发帖子,新手求助 关于验证码的问题
- 关于mybatis的异常 求解决啊~
- Tomcat记忆问题
- 这个问题离奇~~~~就是关于设置errorPage的
唉 郁闷啊
原因应该是所用的pageContext出错。试试用另外一种方式得到pageContext。
我在别人机子上运行成功后,就把自己机子上的tomcat和jdk卸载了,装上和别人机子上同样版本的tomcat和jdk,不过发现还是报同样的错误。这时无意中我打开目录C:\jdk\jre\lib\ext\,发现我以前放在里面的servlet.jar和smartupload.jar文件,可能这两个文件与我在环境变量里配置的CLASSPATH: C:\jdk\lib\tools.jar;C:\jdk\lib\dt.jar;C:\tomcat\common\lib\servlet-api.jar以及classes里面的com.jspsmart.upload类文件重复,当我停止tomcat后删掉了这两个文件,再启动tomcat运行就能正常上传了。