姬長信(Redy)

java.lang.ClassNotFoundException:org.apache.commons.f…


我试图通过apache tomcat 8.0.14编译UploadServlet servlet.
我的web.xml很好andUploadServlet.java已正确编译,但是当尝试运行时我得到以下错误

错误

HTTP Status 500 - Error instantiating servlet class UploadServlet

exception
javax.servlet.ServletException: Error instantiating servlet class UploadServlet

root cause
java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory

root cause
java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory

UploadServlet.java

import java.io.*;
import java.util.*;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.output.*;

public class UploadServlet extends HttpServlet {

   private boolean isMultipart;
   private String filePath;
   private int maxFileSize = 50 * 1024;
   private int maxMemSize = 4 * 1024;
   private File file ;

   public void init( ){

      filePath =  getServletContext().getInitParameter("file-upload"); 
   }
   public void doPost(HttpServletRequest request, 
               HttpServletResponse response)
              throws ServletException, java.io.IOException {

      isMultipart = ServletFileUpload.isMultipartContent(request);
      response.setContentType("text/html");
      java.io.PrintWriter out = response.getWriter( );
      if( !isMultipart ){
         out.println("");
         out.println("");
         out.println("Servlet upload");  
         out.println("");
         out.println("");
         out.println("

No file uploaded

"); out.println(""); out.println(""); return; } DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(maxMemSize); factory.setRepository(new File("C://apache-tomcat-8.0.14//temp")); ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax( maxFileSize ); try{ List fileItems = upload.parseRequest(request); Iterator i = fileItems.iterator(); out.println(""); out.println(""); out.println("Servlet upload"); out.println(""); out.println(""); while ( i.hasNext () ) { FileItem fi = (FileItem)i.next(); if ( !fi.isFormField () ) { String fieldName = fi.getFieldName(); String fileName = fi.getName(); String contentType = fi.getContentType(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); System.out.println(fileName); if( fileName.lastIndexOf("//") >= 0 ) { file = new File( filePath + fileName.substring(fileName.lastIndexOf("//"))) ; } else { file = new File( filePath + fileName.substring(fileName.lastIndexOf("//")+1)) ; } fi.write( file ) ; out.println("Uploaded Filename: " + fileName + "
"); } } out.println(""); out.println(""); }catch(Exception ex) { System.out.println(ex); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { throw new ServletException("GET method used with " + getClass( ).getName( )+": POST method required."); } }