bHaRaTh
bHaRaTh

Reputation: 3514

File upload problem using Apache commans File upload API in JSP/Servlet

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

Upvotes: 2

Views: 3510

Answers (1)

BalusC
BalusC

Reputation: 1109532

java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream

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.

Upvotes: 1

Related Questions