ethar turky
asked on
Upload files
Dear all ,
how can upload file with jsp?
full code example will be highly appreciated...
Thanks.
how can upload file with jsp?
full code example will be highly appreciated...
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
How to install such a thing?
how to install those .class
how to install those .class
copy the jar into WEB-INF/lib
ASKER
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.isMultipartCont ent(reques t);
// if not, send to message page with the error message
if(!isMultipart){
request.setAttribute("msg" , "Request was not multipart!");
request.getRequestDispatch er("msg.js p").forwar d(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(reques t);
// 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().g etRealPath ("/") + "x/MemberImages", fullFile.getName());
item.write(savedFile);
}
}
// finally send to the msg page
request.getRequestDispatch er("msg.js p").forwar d(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\MemberI mages (Access is denied)
description The requested resource (C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\x\MemberI mages (Access is denied)) is not available.
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
<%@ 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.isMultipartCont
// if not, send to message page with the error message
if(!isMultipart){
request.setAttribute("msg"
request.getRequestDispatch
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(reques
// 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"
} else {
// the item must be an uploaded file
// save it to disk
File fullFile = new File(item.getName());
File savedFile = new File(getServletContext().g
item.write(savedFile);
}
}
// finally send to the msg page
request.getRequestDispatch
%>
It upload file successfuly but give this error:
type Status report
message C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\x\MemberI
description The requested resource (C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\x\MemberI
--------------------------
does that directory exist, and do you have permissions to access it?
ASKER
yes, and the file succesfuly uploade it.
ASKER
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
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
ASKER
msg.jsp:
<%
String msg = (String)request.getAttribu te("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().g etRealPath ("/") + "x/MemberImages", fullFile.getName());
//item.write(savedFile);
and it's work ok,
<%
String msg = (String)request.getAttribu
if(msg != null)
out.println("<font size=+1>" + msg + "</font><br/>");
%>
Click <a href="fileuploaddemo.htm">
I remarked
// File savedFile = new File(getServletContext().g
//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.
ASKER
can't find any commons-fileupload-1.0-dev