使用Apache命令的文件上传问题JSP/Servlet中的文件上传API
我正在尝试使用 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" >
谢谢
im trying to upload a file using org.apache.commons.fileupload. but i dont no, what mistake i have made im getting the following error in my servlet. please any one help me on this..this is the error im getting.
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)
this is my servlet code
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.");
}
and form
<form method="POST" action="upload1" enctype="multipart/form-data" >
thank u
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它只是告诉您运行时类路径中缺少提到的类。正如包名称所暗示的那样,它是 Apache Commons IO 的一部分。您可以下载它来自 http://commons.apache.org/io。解压下载的 zip,将 JAR 文件放入
/WEB-INF/lib
中,重建/重新部署/重新启动 web 应用程序/服务器,此错误应该会消失。It is just telling you that the mentioned class is missing in the runtime classpath. As the package name hints, it's part of Apache Commons IO. You can download it from http://commons.apache.org/io. Extract the downloaded zip, put the JAR file in
/WEB-INF/lib
, rebuild/redeploy/restart the webapp/server and this error should disappear.