Link to home
Start Free TrialLog in
Avatar of ethar turky
ethar turkyFlag for Saudi Arabia

asked on

Upload files

Dear all ,
how can upload file with jsp?

full code example will be highly appreciated...

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ethar turky

ASKER

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 !!
How to install such a thing?
how to install those .class
copy the jar into WEB-INF/lib
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.


--------------------------------------------------------------------------------
does that directory exist, and do you have permissions to access it?
yes, and the file succesfuly uploade it.
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

problem may actually be in msg.jsp
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,
and check your ser \ver logs, they may provide youu with more useful information about what is happening.