• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to upload image file.

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use JSPSmartUpLoad.
http://www.jspsmart.com
Cheers
 
Rajesh Pathak
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Struts also provides nice file upload functionality. Everything is encapsulated for you quite nicely (similar to the COS library).
reply
    Bookmark Topic Watch Topic
  • New Topic