Upload files

Dear all ,
how can upload file with jsp?

full code example will be highly appreciated...

Thanks.
ethar1Asked:
Who is Participating?
 
ethar1Author Commented:
Example Applications: fileuploaddemo.jsp, fileuploaddemo.htm, msg.jsp. Requires commons-fileupload-1.0-dev.jar in the WEB-INF/lib directory of your application on the server.

can't find any commons-fileupload-1.0-dev.jar !!
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ethar1Author Commented:
How to install such a thing?
how to install those .class
0
 
objectsCommented:
copy the jar into WEB-INF/lib
0
 
ethar1Author Commented:
when I use this code  to upload file:
<%@ page import="fileupload.*, java.util.List, java.io.File, java.util.Iterator" %>
<%
      
      // first check if the upload request coming in is a multipart request
      boolean isMultipart = FileUpload.isMultipartContent(request);
      
      // if not, send to message page with the error message
      if(!isMultipart){
            request.setAttribute("msg", "Request was not multipart!");
            request.getRequestDispatcher("msg.jsp").forward(request, response);
            return;
      }
      
      // now lets create a handler for the upload request.
      DiskFileUpload upload = new DiskFileUpload();
      
      // parse this request by the handler
      // this gives us a list of items from the request
      List items = upload.parseRequest(request);
      
      // now iterate over this list
      Iterator itr = items.iterator();
      
      while(itr.hasNext()){
      
            FileItem item = (FileItem) itr.next();
            
            // check if the current item is a form field or an uploaded file
            if(item.isFormField()){
                  
                  // get the name of the field
                  String fieldName = item.getFieldName();
                  
                  // if it is name, we can set it in request to thank the user
                  if(fieldName.equals("name"))
                        request.setAttribute("msg", "Thank You: " + item.getString());
                        
            } else {
            
                  // the item must be an uploaded file
                  // save it to disk                  
                  File fullFile = new File(item.getName());                  
                  File savedFile = new File(getServletContext().getRealPath("/") + "x/MemberImages", fullFile.getName());
                  item.write(savedFile);
            }
      }
      
      // finally send to the msg page
      request.getRequestDispatcher("msg.jsp").forward(request, response);

      
%>


It upload file successfuly but give this error:

type Status report

message C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\x\MemberImages (Access is denied)

description The requested resource (C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\x\MemberImages (Access is denied)) is not available.


--------------------------------------------------------------------------------
0
 
objectsCommented:
does that directory exist, and do you have permissions to access it?
0
 
ethar1Author Commented:
yes, and the file succesfuly uploade it.
0
 
ethar1Author Commented:
This error some time:
type Status report

message /x/fileuploaddemo.jsp

description The requested resource (/x/fileuploaddemo.jsp) is not available.

btw,I own the server

0
 
objectsCommented:
problem may actually be in msg.jsp
0
 
ethar1Author Commented:
msg.jsp:
<%
 
  String msg = (String)request.getAttribute("msg");
 
  if(msg != null)
        out.println("<font size=+1>" + msg + "</font><br/>");
%>

Click <a href="fileuploaddemo.htm">here</a> to go to the upload page.


I remarked
                  // File savedFile = new File(getServletContext().getRealPath("/") + "x/MemberImages", fullFile.getName());
                  //item.write(savedFile);

and it's work ok,
0
 
objectsCommented:
and check your ser \ver logs, they may provide youu with more useful information about what is happening.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.