Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

file upload with jakarta common upload package

843841May 18 2007 — edited May 21 2007
i got the code f file upload from the forum but it is not working and giving some errors . I am attaching the code iam using and the stack trace of the errors i am getting . Please if somebody can help me ............
<html>
<form method="post" action="/servlet/UploadFile" enctype="multipart/form-data">
Name
<input type="text" name="uname"/>
File
<input type="file" name="upfile"/>
<input type="submit"/>
</form>
</html>
and the servlet handling the request is as import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;


/**
* @author sm23772
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class UploadFile extends HttpServlet {

public void doPost(HttpServletRequest req,HttpServletResponse res)
public class UploadFile extends HttpServlet {

public void doPost(HttpServletRequest req,HttpServletResponse res)
{
try{

FileUpload fup=new FileUpload();
boolean isMultipart = FileUpload.isMultipartContent(req);
// Create a new file upload handler
System.out.println(isMultipart);
DiskFileUpload upload = new DiskFileUpload();

// Parse the request
List /* FileItem */ items = upload.parseRequest(req);

Iterator iter = items.iterator();
while (iter.hasNext()) {

FileItem item = (FileItem) iter.next();

if (item.isFormField()) {
System.out.println("its a field");
} else {
System.out.println("its a file");
System.out.println(item.getName());
File cfile=new File(item.getName());
File tosave=new File(getServletContext().getRealPath("/"),cfile.getName());
}
}

}catch(Exception e){System.out.println(e);}
}
}
the exception arising are
as ..........
exception

javax.servlet.ServletException: Cannot allocate servlet instance for path /servlet/UploadFile
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:389)
org.apache.catalina.servlets.InvokerServlet.doPost(InvokerServlet.java:170)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause

java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUpload
java.lang.Class.getDeclaredConstructors0(Native Method)
java.lang.Class.privateGetDeclaredConstructors(Class.java:2328)
java.lang.Class.getConstructor0(Class.java:2640)
java.lang.Class.newInstance0(Class.java:321)
java.lang.Class.newInstance(Class.java:303)
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:370)
org.apache.catalina.servlets.InvokerServlet.doPost(InvokerServlet.java:170)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 18 2007
Added on May 18 2007
2 comments
1,536 views