File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to upload image file.

 
Rajesh Pathak
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rebecca Conroy
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would highly recommend reading the following articleUpload file bean
The main points are:
Client-Side
-----------
* form must be of enctype multipart/form-data
* use the <INPUT type="file"> element for file upload
Server-Side
-----------
* 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.
 
Vijayanandh
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Try this URL. It provides you with an easy interface (MultipartRequest) for handling file uploads.
http://www.servlets.com/cos/index.html.
Also in the client side you should use the
<input type = file ...> element to enable file uploading instead of normal <input type = text ...>.
 
Rebecca Conroy
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all the form should have the following:
enctype="multipart/form-data"
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
JSPSmartUpload
I've used jspsmartupload to upload images in a servlet. Have a look at these classes and good luck.
 
L Goundalkar
Ranch Hand
Posts: 395
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use JSPSmartUpLoad.
http://www.jspsmart.com
Cheers
 
Rajesh Pathak
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot everyone!
While exploring your mentioned links, I found this related link useful as well,
http://www.servlets.com/cos/javadoc/com/oreilly/servlet/MultipartRequest.html
 
James Carman
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Struts also provides nice file upload functionality. Everything is encapsulated for you quite nicely (similar to the COS library).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic