File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes How to upload image file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to upload image file." Watch "How to upload image file." New topic

How to upload image file.

Rajesh Pathak
Ranch Hand

Joined: Aug 15, 2002
Posts: 56
I have made two Text box in a JSP page which will prompt user to enter his name and upload his photograph.
My question is:
1) How an image is uploaded (I mean what will the code for it).
2) I 'trap' user name in a temporary variable by using request.getParameter(name)
How to trap the .gif/.bmp image file?
3) How to save it in database so that it can be displayed later on?
Thanks in advance.

Rajesh(Bangalore,India)<br />SCJP2, SCWCD, SCEA, IBM-XML, UML-OOAD, IBM-Enterprise Connectivity with J2EE.
Rebecca Conroy

Joined: May 27, 2002
Posts: 2
I would highly recommend reading the following articleUpload file bean
The main points are:
* form must be of enctype multipart/form-data
* use the <INPUT type="file"> element for file upload
* Servlets cannot handle multipart/form-data, so need to write own code to handle the stream (the article gives sample code)
* Once you have extracted the file from the servlet input stream, you can write to database
You may also like to check out the following article Filter code for Servlet 2.3 which describes how you can use a file upload filter which overrides the getParameter() methods to be able to get file parameters.
The articles are quite explanatory, and you can either write your code adapted from the articles, or use the com.oreilly.servlet.* package which already implements most of this functionality.

Joined: Jul 23, 2002
Posts: 7
Try this URL. It provides you with an easy interface (MultipartRequest) for handling file uploads.
Also in the client side you should use the
<input type = file ...> element to enable file uploading instead of normal <input type = text ...>.

regards,<br />Vijay
Rebecca Conroy

Joined: May 27, 2002
Posts: 2
I forgot to mention explicitly that the support of filters is new functionality that has been included in servlet spec 2.3; older servlet specs don't have this functionality. (Tomcat 4.0 uses servlet spec 2.3; Tomcat 3.2 uses servlet spec 2.2)
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
First of all the form should have the following:
this is very different from a simple form. In these cases you use predefined classes to get the parameters from the form:
The two classes I know are:
oreilly classes
I've used jspsmartupload to upload images in a servlet. Have a look at these classes and good luck.

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Use JSPSmartUpLoad.

Rajesh Pathak
Ranch Hand

Joined: Aug 15, 2002
Posts: 56
Thanks a lot everyone!
While exploring your mentioned links, I found this related link useful as well,
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Struts also provides nice file upload functionality. Everything is encapsulated for you quite nicely (similar to the COS library).

James Carman, President<br />Carman Consulting, Inc.
I agree. Here's the link:
subject: How to upload image file.
It's not a secret anymore!