首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在JSP/Servlet中使用Apache命令文件上载API的文件上载问题

在JSP/Servlet中使用Apache命令文件上载API的文件上载问题
EN

Stack Overflow用户
提问于 2011-03-26 01:02:11
回答 1查看 3.4K关注 0票数 2

我正在尝试使用org.apache.commons.fileupload上传文件。但是我不知道,我犯了什么错误,我在我的servlet中得到了以下错误。请任何人在this..this上帮助我,这是我得到的错误。

代码语言:javascript
复制
     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代码

代码语言:javascript
复制
   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.");
     }

和表单

代码语言:javascript
复制
          <form method="POST" action="upload1" enctype="multipart/form-data" >

谢谢你

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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/服务器,这个错误应该会消失。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5435671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档