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

bHaRaTh
  • 3,464
  • 4
  • 34
  • 32

1 Answers1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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. – bHaRaTh Mar 25 '11 at 17:24
  • 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](http://commons.apache.org/fileupload/faq.html#whole-path-from-IE) 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](http://stackoverflow.com/questions/81180/how-to-get-the-file-path-from-html-input-form-in-firefox-3/3374408#3374408). – BalusC Mar 25 '11 at 17:26