I'm developing a servlet to handle file upload requests. Despite importing all required classes in the build path, I still receive the 'classNotFoundException' for FileItemFactory. I have also included the file-upload library in the build path of my project. Does anyone know why this is happening? As you can see below I've done a lot of redundant importing as well hoping it would solve the problem but it hasn't. I've been trying to create new projects, etc but I still get the same error.
In the doPost() method I've written my code as follows:
Expand|Select|Wrap|Line Numbers
- public void doPost( HttpServletRequest req, HttpServletResponse res )
- {
- File disk = null;
- FileItem item = null;
- FileItemFactory factory = new DiskFileItemFactory();
- java.util.Iterator iter = null;
- List items = null;
- ServletFileUpload upload = new ServletFileUpload( factory );
- try
- {
- items = upload.parseRequest( req );
- iter = items.iterator();
- while( iter.hasNext() )
- {
- // Get the current item in the iteration
- item = ( FileItem )iter.next();
- disk = new File(item.getName());
- item.write( disk );
- }
- }
- catch( IOException ioe ) {
- ioe.printStackTrace();
- } catch( Exception e ) {
- e.printStackTrace();
- }
- }
Expand|Select|Wrap|Line Numbers
- import java.io.IOException;
- import java.io.File;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.fileupload.servlet.ServletFileUpload;
- import org.apache.commons.fileupload.disk.DiskFileItemFactory;
- import org.apache.commons.fileupload.FileItemFactory;
- import org.apache.commons.fileupload.FileItem;
- import org.apache.commons.fileupload.FileUpload;
- import org.apache.commons.*;
- import org.apache.commons.fileupload.*;
- import java.util.List;
- import org.apache.commons.io.*;
Thanks and regards,
Nikhil