import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class TestServlets extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Hello<br/>");
boolean isMultipartContent = ServletFileUpload
.isMultipartContent(request);
if (!isMultipartContent) {
out.println("You are not trying to upload<br/>");
return;
}
out.println("You are trying to upload<br/>");
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> fields = upload.parseRequest(request);
out.println("Number of fields: " + fields.size() + "<br/><br/>");
Iterator<FileItem> it = fields.iterator();
if (!it.hasNext()) {
out.println("No fields found");
return;
}
out.println("<table border=\"1\">");
while (it.hasNext()) {
out.println("<tr>");
FileItem fileItem = it.next();
boolean isFormField = fileItem.isFormField();
if (isFormField) {
out.println("<td>regular form field</td><td>FIELD NAME: "
+ fileItem.getFieldName() + "<br/>STRING: "
+ fileItem.getString());
out.println("</td>");
} else {
out.println("<td>file form field</td><td>FIELD NAME: "
+ fileItem.getFieldName() + "<br/>STRING: "
+ fileItem.getString() + "<br/>NAME: "
+ fileItem.getName() + "<br/>CONTENT TYPE: "
+ fileItem.getContentType()
+"<br/>SIZE(BYTES): "
+ fileItem.getSize() + "<br/>TO STRING: "
+ fileItem.toString());
out.println("</td>");
}
out.println("</tr>");
}
out.println("</table>");
} catch (FileUploadException e) {
e.printStackTrace();
}
}
}
错误代码
>Mar 27, 2014 2:28:00 PM org.apache.catalina.core.StandardWrapperValve invoke
>SEVERE: Servlet.service() for servlet [TestServlets] in context with path [/First] threwexception >[Servlet execution threw an exception] with root cause
>java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest
> at java.net.URLClassLoader$1.run(Unknown Source)
> at java.net.URLClassLoader$1.run(Unknown Source)
> at java.security.AccessController.doPrivileged(Native Method)
> at java.net.URLClassLoader.findClass(Unknown Source)
> at java.lang.ClassLoader.loadClass(Unknown Source)
> at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
> at java.lang.ClassLoader.loadClass(Unknown Source)
> at org.apache.commons.fileupload.servlet.ServletFileUpload.
> isMultipartContent(ServletFileUpload.java:68)
> at TestServlets.doPost(TestServlets.java:31)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
> at org.apache.catalina.core.ApplicationFilterChain.
> internalDoFilter(ApplicationFilterChain.java:305)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
> at LogFilter.doFilter(LogFilter.java:35)
> at org.apache.catalina.core.ApplicationFilterChain.
> internalDoFilter(ApplicationFilterChain.java:243)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
> at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
> at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
> at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
> at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
> at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
> at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
> at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
> at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
> at org.apache.coyote.http11.AbstractHttp11Processor.
> process(AbstractHttp11Processor.java:999)
> at org.apache.coyote.AbstractProtocol$
> AbstractConnectionHandler.process(AbstractProtocol.java:565)
> at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1812)
> at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
> at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:1)
您忘了添加apache common fileupload jar 。 http://apache.mirrors.lucidnetworks.net//commons/fileupload/binaries/commons-fileupload-1.3.1-bin.zip
下载并添加您的类路径。如果还没有工作,请查看 servlet-api.jar
http://mirrors.ibiblio.org/pub/mirrors/maven/javax.servlet/jars/servlet-api-2.3.jar