java.lang.ClassNotFoundException:org.apache.commons.fileupload.FileItemFactory
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java.lang.ClassNotFoundException:org.apache.commons.fileupload.FileItemFactory,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3404字,纯文字阅读大概需要5分钟。
内容图文
我试图通过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("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>No file uploaded</p>");
out.println("</body>");
out.println("</html>");
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("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
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 + "<br>");
}
}
out.println("</body>");
out.println("</html>");
}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.");
}
}
解决方法:
您需要将commons-fileupload.jar和commons-io.jar放到WEB-INF / lib forlder中.在构建期间使用Classpath,但它们也必须在运行时可用.
实际上,如果您使用IDE进行Java EE开发(如Eclipse),将这些Jars放到WEB-INF / lib就足够了,因为它们会在构建的claspath中自动显示.
内容总结
以上是互联网集市为您收集整理的java.lang.ClassNotFoundException:org.apache.commons.fileupload.FileItemFactory全部内容,希望文章能够帮你解决java.lang.ClassNotFoundException:org.apache.commons.fileupload.FileItemFactory所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。