2

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi thank u very much BalusC .. sorry for asking such a stupid question. i forgot to add commons IO jar on my class path...but another problem is im printing the file name but it is not showing the full path name.
No problem. Hope you've at least learnt how to interpret exceptions. As to the filename, the full path should be not of your interest. Even more, you are supposed to trim it off using FilenameUtils.getName(item.getName()) because the buggy MSIE browser sends the full path along! The path is useless. Just get the file content as InputStream by item.getInputStream(). See also this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.