Thursday, September 11, 2008

Uploading file in JSP

I am writing this article regarding “FILE UPLOADING in JSP from Client-Server” as I have faced problem in my project regarding this issue & help others so tht they don’nt face problem.

Following are the steps for uploading file from jsp:
1) Goto javazoom.net, then download latest version for upload the files.
http://www.javazoom.net/jzservlets/uploadbean/uploadbean.html
2) Extract the zip & u will be getting four jar files.
They are:
i)uploadbean.jar
ii)struts.jar
iii)fileupload.jar
iv)cos.jar
place all these jar files in lib of u r web application:
C:\jakarta-tomcat-5.0.25\webapps\URWEBAPP\WEB-INF\lib\ or
(tomcat)\lib\
i.e. the lib folder.
lib folder varies with tomcat version.Somewhere it will under WEB-INF or outside WEB-INF.
Pl verify the same for ur tomcat version.
3) Set it in class path
4) create one jsp file named SimpleUpload.jsp
5) copy the following action part in the above jsp file:

Please put the html tags as needed.I am unable to gv the html tags as it is allowing me post the source code with html tags.

page language="java" import="javazoom.upload.*,java.util.*"
jsp:useBean id="upBean" scope="page" class="javazoom.upload.UploadBean"
jsp:setProperty name="upBean" property="folderstore" value="c:/uploads"
jsp:useBean
'///// Set the folder where u want upload the particular file.(here it is c:/uploads)

if (MultipartFormDataRequest.isMultipartFormData(request))
{ // Uses MultipartFormDataRequest to parse the HTTP request. MultipartFormDataRequest mrequest = new MultipartFormDataRequest(request); String todo = null;
if (mrequest != null) todo = mrequest.getParameter("todo");
if ( (todo != null) && (todo.equalsIgnoreCase("upload")) )
{
Hashtable files = mrequest.getFiles();
if ( (files != null) && (!files.isEmpty()) )
{
UploadFile file = (UploadFile) files.get("uploadfile");
if (file != null)
out.println("Form field : uploadfile"+"
Uploaded file : "+file.getFileName()+" ("+file.getFileSize()+" bytes)"+"
Content Type : "+file.getContentType());
// Uses the bean now to store specified by jsp:setProperty at the top.
upBean.store(mrequest, "uploadfile");
}
else
{
out.println("No uploaded files");
}
}
else out.println("todo="+todo);
}


Follow the above steps and file upload will work fine.This has worked for me.

Regards
Subharthee

No comments:

No end of this road

No end of this road

WWF Member

WWF Member