我正在尝试使用org.apache.commons.fileupload上传文件。但是我不知道,我犯了什么错误,我在我的servlet中得到了以下错误。请任何人在this..this上帮助我,这是我得到的错误。
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:199)
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:361)
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
upload1.doPost(upload1.java:34)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)这是我的servlet代码
if (ServletFileUpload.isMultipartContent(req)) {
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
try {
List<FileItem> items = upload.parseRequest(req);
for (FileItem item : items) {
// process only file upload - discard other form item types
if (item.isFormField()) continue;
String fileName = item.getName();
// get only the file name not whole path
if (fileName != null) {
// fileName = FilenameUtils. getName(fileName);
}
}
} catch (Exception e) {
res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"An error occurred while creating the file : " + e.getMessage());
}
} else {
res.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,
"Request contents type is not supported by the servlet.");
}和表单
<form method="POST" action="upload1" enctype="multipart/form-data" >谢谢你
发布于 2011-03-26 01:04:00
org/apache/commons/io/output/DeferredFileOutputStream:
java.lang.NoClassDefFoundError
它只是告诉您在运行时类路径中缺少提到的类。正如包名所暗示的,它是Apache Commons IO的一部分。您可以从http://commons.apache.org/io对其进行download。解压下载的压缩包,将JAR文件放入/WEB-INF/lib,重新构建/重新部署/重新启动webapp/服务器,这个错误应该会消失。
https://stackoverflow.com/questions/5435671
复制相似问题