通过表单提交给Servlet表单页面内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>完成页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h1 align="center">报名成功!</h1>
<form method="post" action="/supergirl/uploadphoto" enctype="multipart/form-data">
<table>
<tr>
<td>赶快上传照片吧!</td><td><input type="file" name="photo" size="40">
</tr>
</table>
<center><input type="submit" value="上传"></center>
</form>
</body>
</html>servlet代码如下:
public class uploadPhoto extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
long MAX_SIZE = 3 * 1024 * 1024;
final String[] allowedExt = new String[] {"jpg", "jpeg", "gif"};
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
factory.setRepository(new File(getServletContext().getRealPath("/") + "ImagesUploadTemp"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(MAX_SIZE);
PrintWriter out = response.getWriter();
List<FileItem> fileList = null;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException e) {
if (e instanceof SizeLimitExceededException) {
out.println("文件尺寸超过规定大小:" + MAX_SIZE + "字节<p />");
out.println("<a href=\"upload.html\" target=\"_top\">返回</a>");
return;
}
e.printStackTrace();
}
if (fileList == null || fileList.size() == 0) {
out.println("请选择上传文件<p />");
out.println("<a href=\"upload.html\" target=\"_top\">返回</a>");
return;
}
Iterator<FileItem> fileItr = fileList.iterator();
while (fileItr.hasNext()) {
FileItem fileItem = null;
String path = null;
long size = 0;
fileItem = fileItr.next();
if (fileItem == null || fileItem.isFormField())
continue;
path = fileItem.getName();
size = fileItem.getSize();
if ("".equals(path) || size == 0) {
out.println("请选择上传文件<p />");
out.println("<a href=\"upload.html\" target=\"_top\">返回</a>");
return;
}
String t_name = path.substring(path.lastIndexOf("\\") + 1);
String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
int allowFlag = 0;
int allowedExtCount = allowedExt.length;
for (; allowFlag < allowedExtCount; allowFlag++) {
if (allowedExt[allowFlag].equals(t_ext))
break;
}
if (allowFlag == allowedExtCount) {
out.println("请上传以下类型的文件<p />");
for (allowFlag = 0; allowFlag < allowedExtCount; allowFlag++)
out.println("*." + allowedExt[allowFlag]
+ " ");
out.println("<p /><a href=\"upload.html\" target=\"_top\">返回</a>");
return;
}
long now = System.currentTimeMillis();
String prefix = String.valueOf(now);
String u_name = getServletContext().getRealPath("/") + "ImagesUploaded/"
+ prefix + "." + t_ext;
try {
fileItem.write(new File(u_name));
out.println("文件上传成功. 已保存为: " + prefix + "." + t_ext
+ " 文件大小: " + size + "字节<p />");
out.println("<a href=\"upload.html\" target=\"_top\">继续上传</a>");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}错误显示为:
2009-8-10 13:05:07 org.apache.catalina.core.ApplicationContext log
信息: Marking servlet uploadPhoto as unavailable
2009-8-10 13:05:07 org.apache.catalina.core.ApplicationContext log
严重: Error loading WebappClassLoader
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@39ab89
action.uploadPhoto
java.lang.ClassNotFoundException: action.uploadPhoto
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>完成页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h1 align="center">报名成功!</h1>
<form method="post" action="/supergirl/uploadphoto" enctype="multipart/form-data">
<table>
<tr>
<td>赶快上传照片吧!</td><td><input type="file" name="photo" size="40">
</tr>
</table>
<center><input type="submit" value="上传"></center>
</form>
</body>
</html>servlet代码如下:
public class uploadPhoto extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
long MAX_SIZE = 3 * 1024 * 1024;
final String[] allowedExt = new String[] {"jpg", "jpeg", "gif"};
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
factory.setRepository(new File(getServletContext().getRealPath("/") + "ImagesUploadTemp"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(MAX_SIZE);
PrintWriter out = response.getWriter();
List<FileItem> fileList = null;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException e) {
if (e instanceof SizeLimitExceededException) {
out.println("文件尺寸超过规定大小:" + MAX_SIZE + "字节<p />");
out.println("<a href=\"upload.html\" target=\"_top\">返回</a>");
return;
}
e.printStackTrace();
}
if (fileList == null || fileList.size() == 0) {
out.println("请选择上传文件<p />");
out.println("<a href=\"upload.html\" target=\"_top\">返回</a>");
return;
}
Iterator<FileItem> fileItr = fileList.iterator();
while (fileItr.hasNext()) {
FileItem fileItem = null;
String path = null;
long size = 0;
fileItem = fileItr.next();
if (fileItem == null || fileItem.isFormField())
continue;
path = fileItem.getName();
size = fileItem.getSize();
if ("".equals(path) || size == 0) {
out.println("请选择上传文件<p />");
out.println("<a href=\"upload.html\" target=\"_top\">返回</a>");
return;
}
String t_name = path.substring(path.lastIndexOf("\\") + 1);
String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
int allowFlag = 0;
int allowedExtCount = allowedExt.length;
for (; allowFlag < allowedExtCount; allowFlag++) {
if (allowedExt[allowFlag].equals(t_ext))
break;
}
if (allowFlag == allowedExtCount) {
out.println("请上传以下类型的文件<p />");
for (allowFlag = 0; allowFlag < allowedExtCount; allowFlag++)
out.println("*." + allowedExt[allowFlag]
+ " ");
out.println("<p /><a href=\"upload.html\" target=\"_top\">返回</a>");
return;
}
long now = System.currentTimeMillis();
String prefix = String.valueOf(now);
String u_name = getServletContext().getRealPath("/") + "ImagesUploaded/"
+ prefix + "." + t_ext;
try {
fileItem.write(new File(u_name));
out.println("文件上传成功. 已保存为: " + prefix + "." + t_ext
+ " 文件大小: " + size + "字节<p />");
out.println("<a href=\"upload.html\" target=\"_top\">继续上传</a>");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}错误显示为:
2009-8-10 13:05:07 org.apache.catalina.core.ApplicationContext log
信息: Marking servlet uploadPhoto as unavailable
2009-8-10 13:05:07 org.apache.catalina.core.ApplicationContext log
严重: Error loading WebappClassLoader
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@39ab89
action.uploadPhoto
java.lang.ClassNotFoundException: action.uploadPhoto
如果确实存在这个类的话,重新发布下看看
这个是哪儿来的,建议楼主仔细检查一下自己代码
并且看你的servlet是否已经配置在web.xml中