473,605 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Image upload and retrieve from BLOB in jsp

7 New Member
I m completely new to java.Can anybody provide me detailed JSP code to upload image from file control into a BLOB field into DB2 database using servlet and also code for retrieving the image and showing in webpage.please give it with an axample ...Urgently
Feb 19 '08 #1
3 21459
chaarmann
785 Recognized Expert Contributor
I m completely new to java.Can anybody provide me detailed JSP code to upload image from file control into a BLOB field into DB2 database using servlet and also code for retrieving the image and showing in webpage.please give it with an axample ...Urgently
I will not give complete spoon-feed code here, you should do it by yourself. (read the guidelines of this forum). This forum is only for helping with a java problem and not for providing free code.

So I will tell you how to develop your own solution:

just submit an (image-)file with contentType=mul tipart/form-data
and load it into a byte array. Then make a prepared statement to your database and submit the data.

code snippet:
submitting image from webpage (upload):
Expand|Select|Wrap|Line Numbers
  1. <form name=uploadForm action="<%= url %>" method=post enctype="multipart/form-data">
  2. <input type=file>
  3. </form>
code snippet:
retrieving file-data inside a servlet:
Expand|Select|Wrap|Line Numbers
  1. public class UploadServlet extends HttpServlet
  2. {
  3.     public void service(HttpServletRequest request, HttpServletResponse response)
  4.     throws ServletException, IOException
  5.     {
  6.  
  7.         String contentType = request.getContentType();
  8.         if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
  9.         {
  10.             DataInputStream in = new DataInputStream(request.getInputStream());
  11.             int formDataLength = request.getContentLength();
  12.  
  13.             byte dataBytes[] = new byte[formDataLength];
  14.             int byteRead = 0;
  15.             int totalBytesRead = 0;
  16.  
  17.             while (totalBytesRead < formDataLength) 
  18.             {
  19.                 byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
  20. ...
now, to save this dataBytes array into database, you should read about "JDBC" and how to use "prepared statements" (these are the statements with a question mark inside). It's too much to describe here, also a code snippet is too long. If you already know how to save data into database, then just use the column type "BLOB" and do the same.
But it is better not to save it as BLOB inside the database, but as file and only save the reference to this file inside the database.

If you don't care about assigning user-rights for viewing the images, you can directly save the file on the webserver. Then it can be viewed by just providing its's URL (instead of retrieving it from the database)
Feb 19 '08 #2
johnomniflex
1 New Member
I do care about restricting user access to the blobs in my database (else I would have stored them in the file system, as you suggested).

How does one create the JSP code to create an HTML <a> tag? My code already displays the blobs on the screen, but I want my users to be able to select an icon and be offered a download option.

If you are able to help then Thanks !
Dec 4 '08 #3
JosAH
11,448 Recognized Expert MVP
@johnomniflex
You are either hijacking someone else's thread which is considered rude or you are the original poster in which case you should've told us so; just to reduce confusion.

kind regards,

Jos (moderator)
Dec 5 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2448
by: DH | last post by:
I have a "file upload form" that works OK, but I have been unsuccessful in my attempt to also resize the uploaded .JPG (if it is too wide), over-writing the original .JPG, and then create and save a thumbnail.jpg .... all at the same time. Links to a working example would be appreciated. Thanks.
0
2367
by: Paul Hamlington | last post by:
Hello, I've been programming in ASP for a little while now and quite an advanced user, but I have come across an unusual problem in which I need assistance. I have built my own image upload, I have two versions of the binary to string conversion one fast, one slow because some servers use chillisoft and therefore the append function in not accessible for a disconnected recordset.
2
4156
by: Faree | last post by:
Hi, I am a PHP Developer.can any one help me on image upload and display of that image.what i want is uploading an image to a directory ,displaying of that image as a thumbnail.when i click on that image the image should be diplayed in another page. I wrote code for image uploading and as well as display code.but i failed in image resizing when click on that :(
3
2039
by: Michael | last post by:
Hello, I want to write a componant that helps a user upload an image or file. I want something like you see on yahoo's image upload page. It has to work on mac and windows. Can anyone help me get started? Is there any sample code? Thanks, Michael
1
2252
pritipshah
by: pritipshah | last post by:
Hi All, I am using vtiger open source CRM and in that they are using FCKeditor for Image Upload (Resource browser window) but it is not upload or make new folder for image. Vtiger CRM fource to use IE for better performence so from the first day i was using IE7 for editing and testing. After some time today i tried it with Mozilla browser and Image upload works great in this browser, and sudanly i found that Image upload is not...
7
17049
by: mishrarajesh44 | last post by:
hii all Truly telling i hav got this code from net & i am finding error while running the code below.. code:- <?php $idir = "photo/"; // Path To Images Directory $tdir = "photo/thumbs/"; // Path To Thumbnails Directory $twidth = "125"; // Maximum Width For Thumbnail Images
7
3726
by: dragiton | last post by:
SA Upload SQL Database variable types (image upload and storage) I am having trouble with the SA Upload utility. The following code used to work correctly. However, I lost my database and had to rebuild. Does anyone have any suggestions on what I may have wrong. I am not sure if I built my table to store the picture id's correctly. Maybe a field type or something. <form name="UpdatePropertyPicture" method="POST"...
3
1889
by: Bill H | last post by:
I have written many programs for file / image upload over the years in perl, but know that I am working more in php I find I need to be able to do it in php now. I was gonna just do a google serach on how it is done in php, but I though it would be wiser to ask the members of this group how they accomplish it instead of relying on the top most hits on a search that I may be wording wrong. So, can anyone point me to some trusted docs,...
1
4872
by: chennaibala | last post by:
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or any image file while uploading. i develop program,which can upload many file in folder.problem is,am unable to save my file name in to database.because i used same name for all input file type as file. i dont know get name of file.below i presented my coding for ur view.fed up with my coding..pls...
26
2366
matheussousuke
by: matheussousuke | last post by:
May someone help me correct this script? there a few ' and " and ; in the wrong places: <?php include "./comm.inc"; connectdb(); $sql = "SELECT imgid,imgtype FROM tblimage ORDER BY imgid"; $result = @mysql_query($sql) or die(mysql_error());
0
8007
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7937
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8290
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3960
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1546
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.