我尝试使用org.apache.commons.fileupload上传文件。但我不,我犯了什么错误我在我的servlet中得到以下错误。请任何人帮助我...这是我得到的错误。
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" >
谢谢你
答案 0 :(得分:1)
java.lang.NoClassDefFoundError:org / apache / commons / io / output / DeferredFileOutputStream
它只是告诉您在运行时类路径中缺少所提到的类。正如包名称提示的那样,它是Apache Commons IO的一部分。您可以download http://commons.apache.org/io来{{3}}。解压缩下载的zip,将JAR文件放入/WEB-INF/lib
,重建/重新部署/重新启动webapp / server,此错误应该消失。