?
Solved

Upload files

Posted on 2005-04-16
12
Medium Priority
?
624 Views
Last Modified: 2010-04-01
Dear all ,
how can upload file with jsp?

full code example will be highly appreciated...

Thanks.
0
Comment
Question by:ethar1
  • 6
  • 6
12 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 13800168
0
 

Author Comment

by:ethar1
ID: 13800197
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ethar1
ID: 13801380
How to install such a thing?
how to install those .class
0
 
LVL 92

Expert Comment

by:objects
ID: 13802734
copy the jar into WEB-INF/lib
0
 

Author Comment

by:ethar1
ID: 13802909
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
 
LVL 92

Expert Comment

by:objects
ID: 13802926
does that directory exist, and do you have permissions to access it?
0
 

Author Comment

by:ethar1
ID: 13802928
yes, and the file succesfuly uploade it.
0
 

Author Comment

by:ethar1
ID: 13802933
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
 
LVL 92

Expert Comment

by:objects
ID: 13802935
problem may actually be in msg.jsp
0
 

Author Comment

by:ethar1
ID: 13802939
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
 
LVL 92

Expert Comment

by:objects
ID: 13802992
and check your ser \ver logs, they may provide youu with more useful information about what is happening.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Applications for our next round of the Experts Exchange Scholarship Contest are starting to roll in. It made us wonder what our past winners are up to these days. Here's a look at what four winners experienced with the contest and what they're doing…
Exchange administrators are always vigilant about Exchange crashes and disasters that are possible any time. It is quite essential to identify the symptoms of a possible Exchange issue and be prepared with a proper recovery plan. There are multiple…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question